You can use another card (or install) and use the transform to sort on 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 would be better to just use boost multi-index container.
Richard Corden Mar 26 '09 at 11:02 2009-03-26 11:02
source share