No, that is not possible. The comparator is part of the type of the map. The question is no different from asking whether you can change an int
to store floating point numbers.
More importantly, the ordering provided by the comparator is an integral part of the internal structure of the map. If you were to change the ordering, the data structure would no longer be in a consistent state. The only feasible option is to rebuild a new map from the old map's elements with respect to the new order, but that's already possible:
std::map<T, V, Comp1> m1 = /* ... */;
std::map<T, V, Comp2> m2(m1.begin(), m1.end());
Alternatively, you can make a second map of type std::map<std::reference_wrapper<T const>, std::reference_wrapper<V>, Comp2>
and populate it with references to the original map, but ordered according to Comp2
. In that case it is your own responsibility to maintain the two maps in sync. An advanced container like Boost.Multiindex can do this for you in a safe fashion.