Boost Bimap
Storing objects and looking them up using a key is a very common programming chore, and every language has some measure of support for it through native constructs or libraries in the form of dictionaries or lookup tables. In C++, the std::map
and std::multimap
containers (and their unordered variants) provide the lookup table abstraction. Traditionally, such libraries support lookups in one direction. Given a key you can look up a value and this is adequate for many cases. But sometimes, we also need a way to look up a key given a value, and the standard library associative containers are of little help in such cases; what we need there is the Boost Bimap library.
The Boost Bimap library provides bimaps or bidirectional map data structures that allow lookups using keys as well as values. Let us start with an example to get a feel of how it works. We will use a Boost bimap to store names of countries and territories, with their capitals:
Listing 6.8: Using a bimap
1 #include ...