c++ - 如何使bimap中的两个或多个元素成为key

标签 c++ boost-bimap

我想知道是否可以在 bimap 中插入两个或多个元素作为键。我有一个带有一个元素键的 bimap 的最小示例

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <string>
#include <iostream>

int main()
{
  typedef boost::bimap<boost::bimaps::set_of<int>,boost::bimaps::multiset_of<int> > bimap;
  bimap numbers;

  numbers.insert({1, 1});
  numbers.insert({2, 1});
  numbers.insert({3, 8});
  auto it = numbers.left.find(1);


  std::cout << it->first << ":" << it->second << std::endl;
}

现在我可以拥有类似的东西吗

typedef boost::bimap<boost::bimaps::set_of<int>,boost::bimaps::multiset_of<int, int > > bimap;
bimap numbers;
numbers.insert({1, 1, 5});
numbers.insert({2, 1, 1});

最佳答案

一对整数的类型为 std::pair <int, int> (在 C++11 及更高版本中也是 std::tuple <int, int>)

typedef boost::bimap<boost::bimaps::set_of<int>,boost::bimaps::multiset_of<std::pair<int, int > > > bimap;
bimap numbers;
numbers.insert({1, {1, 5}});
numbers.insert({2, {1, 1}});

注意插入中额外的 {}

关于c++ - 如何使bimap中的两个或多个元素成为key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739197/

相关文章:

c++ - 检查迭代器中是否倒数第二

c++ - 在两个派生类之间执行专用代码

c++ - 跟进。是否定义了对 x++ 的返回引用?

c++ - 使用 bimap 中的键访问值

c++ - boost::bimap 中的移位值

c++ - 使用 cerr 修复的链表段错误

c++ - libxml2错误程序c++

c++ - 将 unordered_set_of 与 Boost.Bimap 一起使用时出错

c++ - 使用 TBB 插入一个无序的 boost bimap