Skip to content

Instantly share code, notes, and snippets.

@shintakezou
Created April 14, 2019 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shintakezou/6ca0c723e732a774eb84c36de75d673a to your computer and use it in GitHub Desktop.
Save shintakezou/6ca0c723e732a774eb84c36de75d673a to your computer and use it in GitHub Desktop.
Lambda used as nested functions.
#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;
}
#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
#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