0

'left' 是左侧(已设置)中每个元素的 std::set 的 std::vector,我试图通过迭代'left'来对另一个集合进行设置联合操作。

为什么下面的代码不起作用。我正在尝试做两组的集合。

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_algo.h:5078:错误:分配只读位置'__result.std::_Rb_tree_const_iterator<_Tp>::operator* with _Tp = int'</p>

4

1 回答 1

1

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

如果要添加 to 的内容dummyleft[*u]则插入每个元素:

std::copy(dummy.begin(), dummy.end(), std::inserter(left[*u]));
于 2013-07-19T15:25:24.127 回答