c++ - 是否可以覆盖 boost::bimaps::bimap.left 的 "find"和 "erase"方法?怎么做?

标签 c++ key-value keyvaluepair bimap boost-bimap

我有以下内容:

struct foo_and_number_helper {
  std::string foo;
  uint64_t number;
};
struct foo_and_number {};
struct bar {};

using my_bimap = boost::bimaps::bimap<
  boost::bimaps::unordered_set_of<boost::bimaps::tagged<foo_and_number_helper, foo_and_number>>, 
  boost::bimaps::multiset_of<boost::bimaps::tagged<std::string, bar>>
>;

my_bimap instance;

我希望能够像这样调用查找和删除方法:
instance.left.find("foo")而不是 instance.left.find({"foo",1})
instance.left.erase("foo")而不是 instance.left.erase({"foo",1}) .

我只想使用“foo_and_number_helper”的“foo”部分而不是两个部分用于从左侧调用的方法 find 和 erase。如何实现?我尝试阅读 bimap 实现,但我仍然很难做到。

我已经问了更广泛的问题:Is C++ bimap possible with one side of view having different key than other side of the view value? How to do that? 从评论中我必须覆盖 operator < ,但我什至不确定它是否足够。

最佳答案

我会选择 boost::multi_index_containerboost::bimap 上。

namespace bmi = boost::multi_index;

struct ElementType { 
  std::string foo; 
  std::string bar;
  uint64_t number; 
}

using my_bimap = boost::multi_index_container<
  ElementType,
  bmi::indexed_by<
    bmi::unordered_unique<
      bmi::tagged<struct Foo>, 
      bmi::member<ElementType, std::string, &ElementType::foo>
    >,
    bmi::ordered<
      bmi::tagged<struct Bar>, 
      bmi::member<ElementType, std::string, &ElementType::bar>
    >,
    // and others like
    bmi::sequenced<
      bmi::tagged<struct InsertionOrder>
    >
  >
>;

然后你会像这样使用它

my_bimap instance;

instance.get<Foo>().find("foo");
instance.get<Bar>().erase("bar");
std::cout << instance.get<InsertionOrder>()[10].foo;

即您可以拥有任意数量的 View ,而不是拥有 View

关于c++ - 是否可以覆盖 boost::bimaps::bimap.left 的 "find"和 "erase"方法?怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50700091/

相关文章:

c++ - 解决静态断言中不完整的类型

javascript - Object.assign 返回未定义

php - 如何在php中将对象数组转换为键值对

c# - 如何比较 IsGenericType 定义为 def 的相同类型

c++ - 为什么按位左移将 uint8_t 提升为更宽的类型

c++ - SIGABRT 在线程中访问内存时

c++ - 使用数组对 C++ 中的链表进行排序以对其数据进行排序

javascript - 从成对列表中获取 JSON 值作为数组(用于自动完成功能)

mysql - 管理用户集

ruby - 创建哈希的快捷方式