【问题标题】:How can I use boost::bimap in an unordered and mutable way?如何以无序和可变的方式使用 boost::bimap?
【发布时间】:2014-09-16 15:50:27
【问题描述】:

我正在寻找一个双向无序映射。目前,我只有这个。问题是,我不能使用[]。我认为 boost 默认为列表类型。但我想要一个哈希图。这怎么可能?

#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。但是,一旦插入,我就无法修改值。

【问题讨论】:

    标签: c++ boost hashmap boost-bimap


    【解决方案1】:

    我切换到两个 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2019-11-13
      • 2012-05-31
      相关资源
      最近更新 更多