How to specify custom compare for the following example? The idea is to access member variable within a class for comparison.
struct MyStruct {
unsigned p;
unsigned t;
};
class MyClass {
public:
void call() {
ob["o1_10"] ={10, 1};
mbp[ob["o1_10"].p].insert("o1_10");
ob["o2_20_2"] ={20, 2};
mbp[ob["o2_20"].p].insert("o2_20");
ob["o4_4_4"] ={4, 4};
mbp[ob["o4_4"].p].insert("o4_4");
ob["o5_10"] ={10, 4};
mbp[ob["o5_10"].p].insert("o5_10");
}
private:
map<unsigned,set<string, Compare>> mbp;
// Question: how to define compare using struct, operator(), statice metthod or external method so that
// compare fetches ob[ol] and ob[0l2] and decide based on some rules
// so, comparions is not based on ol and o2 and it is based on some p and t in ob[o1] and ob[02]
// Eg:
// bool compare(const string& o1, const string& o2) const {
// if (ob.at(o1).p > ob.at(o2).p) return true;
// if (ob.at(o1).p == ob.at(o2).p && ob.at(o1).t < ob.at(o2).t) return true;
// return false;
// }
map<string, MyStruct> ob;
};
int main() {
MyClass my_class;
my_class.call();
return 0;
}
It is important to note that comparison keys are not instances of the class. Instead, I want to use the key to access instances of the class to compare. If this can be achieved, I can save some memory.
I have tried the followings and each give me different errors:
- struct within class
- functor
- using bind
You need to overload
operator<
like this.With this operator you can use your class both in
std::set
andstd::map
(as key). In practice you probably also want to overload ALL compare operators (For C++20 overload the spaceship operator instead).