Last active
July 29, 2021 16:43
-
-
Save pranavsharma/52f68750a10c51d131f803fbc2eb1098 to your computer and use it in GitHub Desktop.
Example of using lambda as a comparator for unordered_set
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 <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