1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-01-17 13:38:38 +01:00

[cpp/en] comparator function for std containers

This commit is contained in:
Brook Zhou 2015-10-31 16:17:58 -07:00
parent 5b0db44e7c
commit c3e769e4ac

View File

@ -801,6 +801,24 @@ void doSomethingWithAFile(const std::string& filename)
// all automatically destroy their contents when they fall out of scope. // all automatically destroy their contents when they fall out of scope.
// - Mutexes using lock_guard and unique_lock // - Mutexes using lock_guard and unique_lock
// containers with object keys of non-primitive values (custom classes) require
// compare function in the object itself or as a function pointer. Primitives
// have default comparators, but you can override it.
class Foo {
public:
int j;
Foo(int a) : j(a) {}
};
struct compareFunction {
bool operator()(const Foo& a, const Foo& b) const {
return a.j < b.j;
}
};
//this isn't allowed (although it can vary depending on compiler)
//std::map<Foo, int> fooMap;
std::map<Foo, int, compareFunction> fooMap;
fooMap[Foo(1)] = 1;
fooMap.find(Foo(1)); //true
///////////////////// /////////////////////
// Fun stuff // Fun stuff