Which collection class to use?
-
I have a set of int's and string's that have a one-to-one relationship with each other. There are never any duplicates on either side. Whats the best collection class to hold these pairs so that it is easy to find an entry given either the left or right hand value of the pair? std::map has a find function but it only finds on the 'key' field, I also need the equivalent for the 'value' field.
-
I have a set of int's and string's that have a one-to-one relationship with each other. There are never any duplicates on either side. Whats the best collection class to hold these pairs so that it is easy to find an entry given either the left or right hand value of the pair? std::map has a find function but it only finds on the 'key' field, I also need the equivalent for the 'value' field.
Hi Paul, I wrote a class called
bimap
which seems to me is exactly what you're looking for. Check it out here[^] (Caveat, it does not work for MSVC 7.1.) The original idea ofbimap
, namely to maintain several access orders over the same set of elements, has been much expanded in a Boost library called Boost.MultiIndex[^]. A bidirectional map is just a particular case of what can be achieved with Boost.MultiIndex (though the syntax is not as terse as inbimap
.) Boost.MultiIndex works for MSVC 7.1 and is generally much more powerful, so I'd recommend going for this. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!