集合联合不起作用

set union is not working

本文关键字:不起作用 集合      更新时间:2023-10-16

'left'是std::set的std::向量对于left中的每个元素(已设置),我试图通过在"left"上迭代来对另一个集合执行set并集操作。

为什么以下代码不起作用。我正在试着做两个集合的集合并集。

std::vector<std::set<int> > left(num_nodes);
//Both leftv and left are not empty ....there is some code here which fills them.
std::set<int> leftv, dummy; 
for(std::set<int>::iterator u = leftv.begin(); u != leftv.end() ;u++){
    dummy.insert(v);          //v is some integer
    std::set_union (left[*u].begin(), left[*u].end(), dummy.begin(), dummy.end(), left[*u].begin());
    dummy.clear();
}

错误/usr/include/c++/4.3/bits/stl_alg.h:5078:错误:分配只读位置'__result.std::_Rb_tree_const_iterator&lt_Tp>::运算符*,带_Tp=int’

您试图通过将left[*u].begin()作为set_union的输出参数来覆盖集合的内容。集合中的元素不能修改,因为它们的值决定了它们在集合中的位置。即使可以,您也需要扩展容器以容纳额外的元素,而不是简单地覆盖现有的元素;并且输出必须不与任何一个输入范围重叠。总之:不能使用set_union将一个集合的内容插入到另一个集合中。

如果要将dummy的内容添加到left[*u],则插入每个元素:

std::copy(dummy.begin(), dummy.end(), std::inserter(left[*u]));