jeudi 30 août 2018

Custom compare function for std::multimap when keys are equal

I would like to code a custom comparator for a std::multimap. What I would like to do is to compare the keys, in case they are equal, then compare the values. I'm trying to do it by overloading the operator() in a struct and passing the function object as a third parameter in the std::multimap constructor.

struct CustomComp {
    bool operator()(int key_lhs, int key_rhs){
        if (key_lhs < key_rhs) return true;
        if (key_lhs == key_rhs) //Check values;
        else return false;
    }
};

multimap<int, int, CustomComp> myMap;

How can I access the values, not only the keys, if both are int?

Aucun commentaire:

Enregistrer un commentaire