You can use a different map (or set) and use transform to do the sorting as you insert:
#include <map>
#include <algorithm>
typedef std::map<unsigned int, signed char> MapType1;
typedef std::map<MapType1::mapped_type, MapType1::key_type> MapType2;
struct SwapPair
{
MapType2::value_type operator()(MapType1::value_type const & v)
{
return std::make_pair (v.second, v.first);
}
};
int main ()
{
MapType1 m1;
for(int i = 0; i < 10; ++i)
m1[i] = i * -i;
MapType2 m2;
std::transform (m1.begin ()
, m1.end ()
, std::inserter (m2, m2.end ())
, SwapPair ());
}
I forgot to add that if you need to do this a lot then it might be better just to use a boost multi-index container.