I'm looking for a bidirectional unordered map. Currently, I just have this. The problem is, that I can't use []
. What I think is that boost defaults to list types. But I want a hashmap. How's this possible?
#include <string>
#include <boost/bimap.hpp>
boost::bimap<std::string, size_t> indices;
// ...
size_t index = 42;
indices.right[index].second = "name"; // This doesn't work.
On the overview page, I found out that unordered_set_of
makes the bimap behave like a hashmap. However, I can't modify values once inserted.
I switched over to two std::unordered_map
containers. The down side is that you manually have to keep both in sync. On the other hand, this was much more practical for me since the Boost code got very verbose.
#include <string>
#include <unordered_map>
std::unordered_map<std::string, size_t> indices;
std::unordered_map<size_t, std::string> names;
// ...
size_t index = 42;
std::string name = "John";
indices[name] = index;
names[index] = name;