1

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});
AwaitedOne
  • 992
  • 3
  • 19
  • 42
  • Sorry, I feel dumb, but what do you mean by "two elements"? Don't you blatantly have *three* elements inserted into the map? – Kerrek SB Jan 19 '17 at 10:31
  • @KerrekSB Oh!! sorry if I didn't put it well. I mean http://stackoverflow.com/questions/41675259/unordered-map-to-have-three-elements – AwaitedOne Jan 19 '17 at 10:38
  • 2
    No, wait, please make your question self-contained. I don't want to go on a wild goose chase for meaning. I already do that in my day job. – Kerrek SB Jan 19 '17 at 10:39
  • @KerrekSB I want to have one unique key that I have with `boost::bimaps::set_of` other key (not strictly unique) that I have with `boost::bimaps::multiset_of`. I wounder if I can make one more integer entry i.e something like `boost::bimaps::multiset_of` – AwaitedOne Jan 19 '17 at 10:45

1 Answers1

1

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

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • Thanks for your answer. I can search anything by using an iterator, but how can I get key and its value by using `at(1) or at(2)` – AwaitedOne Jan 24 '17 at 15:50
  • `key` and `value` from which side of the bimap? you can search with e.g. `numbers.right.find({1, 1});` to get 2 – Caleth Jan 24 '17 at 15:54
  • don't want to search. I know my key is there and i want to search something from left like `numbers.left.at(1)` or from right – AwaitedOne Jan 24 '17 at 16:04
  • in the given example, you get a `std::pair` from that, with the value `1, 5`. I don't know what you want if that isn't it – Caleth Jan 24 '17 at 16:09
  • Ya exactly . `numbers.left.at(1)` should give `1,5` and `numbers.right.at(1,5)` is 1 – AwaitedOne Jan 24 '17 at 16:22
  • Have you tried that with my example? Those are the results I expect – Caleth Jan 24 '17 at 16:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133912/discussion-between-caleth-and-awaitedone). – Caleth Jan 24 '17 at 16:27