Domanda

'left' is std::vector of std::set for each element in left(which is set), I am trying to do set union operation to another set by iterating over 'left'.

Why is the following code not working. I am trying to do set union of the two sets.

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();
}

Error /usr/include/c++/4.3/bits/stl_algo.h:5078: error: assignment of read-only location ‘__result.std::_Rb_tree_const_iterator<_Tp>::operator* with _Tp = int’

È stato utile?

Soluzione

You are trying to overwrite the contents of a set, by giving left[*u].begin() as the output argument of set_union. The elements of a set cannot be modified, since their value determines their position in the set. Even if you could, you would need to grow the container to accommodate the extra elements, not simply overwrite the existing ones; and the output must not overlap with either input range. To summarise: you can't use set_union to insert the contents of one set into another.

If you want to add the contents of dummy to left[*u], then insert each element:

std::copy(dummy.begin(), dummy.end(), std::inserter(left[*u]));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top