(Weird this question cannot be asked, because it looks like homework? )
The question is how to define the reset() function in a map with user-defined parameterized comparison struct, which may change the comparison struct for the map. Is it possible at all to define it inside? Should I define copy constructor and other stuff?
I guess indirectly, I could use a pointer outside: every time I need to reset, I just delete the old object and define a new one. But this is ugly. I am wondering whether there are more direct methods.
The following question std::map change key_comp after initialization might be related, but not the same.
I have the following code:
struct MapStruct {
struct My_CMP {
char _dirc;
bool operator() (int a, int b) {
return _dirc == 'X' ? a < b : a > b;
}
};
char _dirc;
map<int, int, My_CMP> _mapStruct;
MapStruct(char dirc): _dric(dirc), _mapStruct(My_CMP(_dirc)) {}
void reset(char dirc) {???}
};