Using set_intersection on set container.

360 views Asked by At

Hi everybody :) i d like to create a personnal set Class and to overload the operator /=, in the case of my class, this operator should be used to take the interstion of two Sets. I got the following error:

error: assignment of read-only location '__result.std::_Rb_tree_const_iterator<_Tp>::operator*()'

Here is the part of the code giving me this error:

Set& operator /=(const Set& st) // Substraction Assignement operator
{
    set<T> tmp;

    set_intersection(m_set.begin(), m_set.end(), st.m_set.begin(), st.m_set.end(), tmp.begin());
    *this = tmp;
    return *this;
}

I m new to c++ and i do not understand where am i trying to assign somehting in read-only location ... Plz can you explain it to me and so tell me how am i supposed to correctly use set_intersection (from library

Sorry for my approximate english, and already thank you for the help =D

1

There are 1 answers

0
Terry Shi On

You need to use an inserter to get it work.

set<T> tmp;

set_intersection(m_set.begin(), m_set.end(),
                 st.m_set.begin(), st.m_set.end(), std::inserter(tmp, tmp.begin()));