Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping keys and values of a map in C++

Tags:

c++

I'm looking for a function in C++ that for swap the contents of a map ... that is: those that were the keys now become the items and those that the items were now the keys. Can you tell me if there is something about this?

like image 621
Safari Avatar asked Oct 23 '11 18:10

Safari


1 Answers

As Geoffroy said, std::map doesn't allow this behaviour. However, you might want to use a STL-like container Boost.Bimap - bidirectorial map.

A Bimap is a data structure that represents bidirectional relations between elements of two collections. The container is designed to work as two opposed STL maps. A bimap between a collection X and a collection Y can be viewed as a map from X to Y (this view will be called the left map view) or as a map from Y to X (known as the right map view).

like image 183
Oleg Svechkarenko Avatar answered Sep 19 '22 16:09

Oleg Svechkarenko