0

How to use a custom comparator for a class std::map variable using lambda function. I'm getting error as error: 'comp' is not a type, std::map<std::pair<std::string, int>, int, decltype(comp)> mapp(comp);. This is my code

#include <iostream>
#include <string>
#include <map>

auto comp = [] (const std::pair<std::string, int>& p1, const std::pair<std::string, int>& p2) {
    if (p1.second > p2.second)
        return true;
    else if (p1.second == p2.second)
        return p1.first < p2.first;

    return false;
};

class A {
    // below also doesn't work
    /*static constexpr auto comp = [] (const std::pair<std::string, int>& p1, const std::pair<std::string, int>& p2) {
        if (p1.second > p2.second)
            return true;
        else if (p1.second == p2.second)
            return p1.first < p2.first;

        return false;
    };*/
    std::map<std::pair<std::string, int>, int, decltype(comp)> mapp(comp);

public:
    A() {

    }

    void add(const std::string& name, int score) {
        
    }

    std::string get() {
        return "";
    }
};

int main() {
}
Harry
  • 2,177
  • 1
  • 19
  • 33

0 Answers0