Find all the keys of repeated element in bimap

598 Views Asked by At

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).

1

There are 1 best solutions below

7
On BEST ANSWER

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.

using ritr = bimap_reference::right_const_iterator;

std::pair<ritr, ritr> range = numbers.right.equal_range(100000);

for (auto itr = range.first; itr != range.second; ++itr)
{
        ...
}


Edit:

The using ritr statement makes ritr an alias for the actual type. It's equivalent to a typedef 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.