如何以无序和可变的方式使用boost :: bimap?

时间:2014-09-16 15:50:28

标签: c++ boost hashmap boost-bimap

我正在寻找双向无序地图。目前,我只是有这个。问题是,我无法使用[]。我认为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。但是,插入后我无法修改值。

1 个答案:

答案 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;