c++boost-bimap

How to make two or more elements in bimap as key


I would like to know if it is possible to insert two or more elements in bimap as key. I have a minimal example of bimap with one element key

#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;
}

Now can I have something like

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});

Solution

  • A pair of ints has type std::pair<int, int> ( also std::tuple<int, int> in C++11 and later )

    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}});
    

    Note the extra {} in the inserts