Add values to a multiset in a boost::bimap

泪湿孤枕 提交于 2020-01-05 07:03:44

问题


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


回答1:


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 


来源:https://stackoverflow.com/questions/42209189/add-values-to-a-multiset-in-a-boostbimap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!