Answers for "how to change comparator of a set in c++"

C++
2

c++ custom comparator for elements in set

struct compare {
    bool operator() (const int& x, const int& y) const {
        return x<y; // if x<y then x will come before y. Change this condition as per requirement
    }
};
int main()
{
  set<int,compare> s; //use the comparator like this
}
Posted by: Guest on December-06-2020
0

c++ set comparator

set<int, less<int>> st;
// or
set<int, greater<int>> st;
// c++ 11
auto cmp = [](int a, int b) { return ... };
set<int, decltype(cmp)> s(cmp);
Posted by: Guest on March-13-2021

Code answers related to "how to change comparator of a set in c++"

Browse Popular Code Answers by Language