samedi 10 avril 2021

Map constructor with comparator

class cmp { // simple comparison function public: bool operator()(const int x,const int y) { return x > y; } };

When i call any stl algorithms like std::sort with an object of compare , it works fine.

std::sort(vec.begin(), vec.end(), cmp());

But for stl containers this is not similar.

std::map<int, int> mp(cmp()); // Not working std::map< int, int, cmp> mp; // Works fine.

Also, in case of lambda expression,

auto cmp = [](cont int x, const int y) { return x > y; };

std::map<int, int, bool(*)(int, int)> mp(cmp()); //Works std::map<int, int > mp(cmp); // Doesnot work

Reading the map constructor from cplusplus does not clear my doubts ? Can anyone explain the difference ?

Aucun commentaire:

Enregistrer un commentaire