Created
April 14, 2019 16:22
-
-
Save shintakezou/6ca0c723e732a774eb84c36de75d673a to your computer and use it in GitHub Desktop.
Lambda used as nested functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Element.hpp" | |
#include <vector> | |
#include <string> | |
size_t count_uniques(const std::vector<SElement> v) | |
{ | |
size_t c = 0; | |
bool found; | |
auto compare = [&v](int i, int j) -> int { | |
if (v[i].e2 == v[j].e2) | |
return 0; | |
else if (v[i].e1 != v[j].e1) | |
return v[j].e1 - v[i].e1; | |
else | |
return v[j].e2 - v[i].e2; | |
}; | |
for (size_t i = 0; i < v.size(); ++i) { | |
found = false; | |
for (size_t j = 0; j < i; ++j) { | |
if (compare(i, j) == 0) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) ++c; | |
} | |
return c; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ELEMENT_H | |
#define ELEMENT_H | |
#include <string> | |
#include <vector> | |
struct SElement | |
{ | |
std::string s; | |
int e1; | |
int e2; | |
}; | |
size_t count_uniques(const std::vector<SElement> v); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Element.hpp" | |
#include <iostream> | |
#include <vector> | |
int main() | |
{ | |
std::vector<SElement> s1{{"zak", 2, 0}, | |
{"mac", 3, 1}, | |
{"abc", 4, 2}, | |
{"zak", 5, 0}}; | |
std::cout << count_uniques(s1) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment