Using custom std::set comparator

后端 未结 5 1555
耶瑟儿~
耶瑟儿~ 2020-11-22 16:03

I am trying to change the default order of the items in a set of integers to be lexicographic instead of numeric, and I can\'t get the following to compile with g++:

5条回答
  •  旧时难觅i
    2020-11-22 16:37

    1. Modern C++20 solution

    auto cmp = [](int a, int b) { return ... };
    std::set s;
    

    We use lambda function as comparator. As usual, comparator should return boolean value, indicating whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.

    Online demo

    2. Modern C++11 solution

    auto cmp = [](int a, int b) { return ... };
    std::set s(cmp);
    

    Before C++20 we need to pass lambda as argument to set constructor

    Online demo

    3. Similar to first solution, but with function instead of lambda

    Make comparator as usual boolean function

    bool cmp(int a, int b) {
        return ...;
    }
    

    Then use it, either this way:

    std::set s(cmp);
    

    Online demo

    or this way:

    std::set s(&cmp);
    

    Online demo

    4. Old solution using struct with () operator

    struct cmp {
        bool operator() (int a, int b) const {
            return ...
        }
    };
    
    // ...
    // later
    std::set s;
    

    Online demo

    5. Alternative solution: create struct from boolean function

    Take boolean function

    bool cmp(int a, int b) {
        return ...;
    }
    

    And make struct from it using std::integral_constant

    #include 
    using Cmp = std::integral_constant;
    

    Finally, use the struct as comparator

    std::set set;
    

    Online demo

提交回复
热议问题