我正在寻找双向无序地图。目前,我只是有这个。问题是,我无法使用[]
。我认为boost默认为列表类型。但我想要一个hashmap。这怎么可能?
#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.
在overview page上,我发现unordered_set_of
使得bimap的行为类似于hashmap。但是,插入后我无法修改值。
答案 0 :(得分:0)
我切换到两个std::unordered_map
容器。不利的一面是你必须手动保持同步。另一方面,这对我来说更实用,因为Boost代码非常详细。
#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;
indices[index] = name;