I want to access all the keys of a repeated element in bimap
. I have some example code below
#include <string>
#include <iostream>
#include <utility>
#include <boost/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
namespace bimaps = boost::bimaps;
typedef boost::bimap<bimaps::set_of<unsigned long int>,
bimaps::multiset_of<unsigned long int > > bimap_reference;
typedef bimap_reference::value_type position;
bimap_reference numbers;
int main()
{
numbers.insert(position(123456, 100000));
numbers.insert(position(234567, 80000));
numbers.insert(position(345678, 100000));
numbers.insert(position(456789, 80000));
auto it = numbers.right.find(100000);
std::cout<<"numbers:"<<it->first<<"<->"<<it->second<<std::endl;
return 0;
}
In the above code I have repeated elements on right side. The above code give me the first key of the element 100000
, i,e 123456 <--> 100000
. But I have one more entry of the element 100000
, how to access all the keys of the repeated element (the element may be present multiple times with unique key on left side).
Use multiset::equal_range on the right hand side.
In this way you get an iterator range from the first occurrence to the last which you need to then iterate.
Edit:
The
using ritr
statement makesritr
an alias for the actual type. It's equivalent to atypedef
which you use higher up.The alias simply makes the next line easier to read than had I typed-out the actual type name twice in the std::pair declaration.
Even an easier solution will be to use auto.
The
equal_range
is a member function of the multiset which is on the right of the bimap. It returns a std::pair which contains the begin and end iterators of elements in the multiset that equal the specified key, for you to iterate in the normal way.