Search code examples
c++dictionarystlswapmultimap

Swapping each pair of values in a multimap<int, int>


I have a multimap containing over 5 million pairs and I need to swap the keys with the values.

unordered_multimap<int, int> edge;

Due to the large size of the container and the processes involved, I would prefer to not have to create a new multimap with the swapped pairs by iterating over each element of the map.

What would be the best way, if any, to do this in place?


Solution

  • The proper approach is not to do this at all, but instead to have a bi-directional map in the first place, on which you can perform lookup in either direction.

    Consider looking into Boost.Bimap.