在boost :: bimap中将值添加到多集 [英] Add values to a multiset in a boost::bimap
问题描述
我想使用boost :: bimap的多图版本,并且我正在遵循此方法,
I wanted to use a multimap version of a boost::bimap and I am following this,
这显示了如何在结构中添加和检索值.我试图根据右侧的值进行查找,该值映射到左侧的多个值,如果找到,我想添加到左侧的列表中.例如,假设这是bimap,
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)
以及执行bimap.left.equal_range(1);
and when you do a bimap.left.equal_range(1);
您得到
1=>1
1=>2
1=>1
1=>2
我想对其进行更新,使其也映射到3,即将3添加到列表中,以便下次完成bimap.left.equal_range(1);
时,将是结果,
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
1=>1
1=>2
1=>3
如何获取右侧的列表,以便可以像上面提到的那样修改列表(而不是仅使用const迭代器,而是仅查看值).
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
推荐答案
只需...添加它:
#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 << " ";
}
打印:
1 2
after adding:
1 2 3
after removing:
1 2
这篇关于在boost :: bimap中将值添加到多集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!