Skip to content

Instantly share code, notes, and snippets.

@pranavsharma
Last active July 29, 2021 16:43
Show Gist options
  • Save pranavsharma/52f68750a10c51d131f803fbc2eb1098 to your computer and use it in GitHub Desktop.
Save pranavsharma/52f68750a10c51d131f803fbc2eb1098 to your computer and use it in GitHub Desktop.
Example of using lambda as a comparator for unordered_set
#include <iostream>
#include <string>
#include <unordered_set>
#include <functional>
#include <assert.h>
using namespace std;
struct Foo {
string name;
};
int main() {
Foo* f1 = new Foo();
f1->name = "foo1";
Foo* f2 = new Foo();
f2->name = "foo2";
auto comp = [](const Foo* a, const Foo* b) {
return a->name == b->name;
};
unordered_set<Foo*, std::hash<Foo*>, decltype(comp)> st(10, std::hash<Foo*>(), comp);
assert(st.insert(f1).second);
assert(! st.insert(f1).second);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment