visual-c++boostbimapboost-bimap

Add values to a multiset in a boost::bimap


I wanted to use a multimap version of a boost::bimap and I am following this,

Boost::Bimap equivalent of bidirectional multimap

This shows how to add and retrieve values in the structure. I am trying to look up based on a value on the right that maps to multiple values on the left, and if found, I would like to add to the list on the left. For example, assume, this is the bimap,

value_type(1, 1)
value_type(10, 50) 
value_type(1, 2)
value_type(9, 15)

and when you do a bimap.left.equal_range(1);

you get

1=>1 1=>2

I would like to update it such that it also maps to 3, ie, add 3 to the list, so that next time when bimap.left.equal_range(1); is done, this would be the result,

1=>1 1=>2 1=>3

How can I get the list on the right so that I can modify the list like mentioned above(instead of just a const iterator, to just view the values).

TIA


Solution

  • Just... add it:

    Live On Coliru

    #include <iostream>
    #include <boost/bimap.hpp>
    #include <boost/bimap/multiset_of.hpp>
    #include <boost/bimap/set_of.hpp>
    #include <boost/range/iterator_range.hpp>
    
    namespace bimaps = boost::bimaps;
    
    int main() {
        typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t;
    
        bimap_t m;
    
        m.insert({1, 1});
        m.insert({10, 50});
        m.insert({1, 2});
        m.insert({9, 15});
    
        for (auto p : boost::make_iterator_range(m.left.equal_range(1)))
            std::cout << p.second << " ";
    
        std::cout << "\nafter adding:\n";
    
        m.insert({1, 3});
    
        for (auto p : boost::make_iterator_range(m.left.equal_range(1)))
            std::cout << p.second << " ";
    
        std::cout << "\nafter removing:\n";
    
        m.right.erase(m.right.find(3));
    
        for (auto p : boost::make_iterator_range(m.left.equal_range(1)))
            std::cout << p.second << " ";
    }
    

    Prints:

    1 2 
    after adding:
    1 2 3 
    after removing:
    1 2