Erase-remove idiom with std::set failing with constness-related error?

In std::set the elements are not modifiable. So, the std::set::iterator is also unmodifiable. From this tutorial, section 27.3. 2.1.

In std::set, the elements are not modifiable. So, the std::set::iterator is also unmodifiable. From this tutorial, section 27.3.2.1: In simple associative containers, where the elements are the keys, the elements are completely immutable; the nested types iterator and const_iterator are therefore the same.

Hence, the erase-remove idiom cannot be applied as is. You have to write a for loop, and use the member function std::set::erase inside it. See this question and this accepted answer and another answer for exact details, but in short, the loop is like the following typename std::set::iterator set_iter; for( set_iter it = s.begin(); it!

= s.end(); /* blank */ ) { if( some_condition() ) { s. Erase( it++ ); // Note the subtlety here } else { ++it; } }.

Erase-remove idiom cannot be used with associative containers. Associative containers do not allow modifications of the entire container element through the iterator, which immediately means that mutating sequence operations (like std::remove) cannot be applied to them.

If I remember well, std::remove is never to be used with a std::set element. As a set is not a pure array you have to use erase.

As already said your code doesn't work because you try to modify a sequence inside an associative container, but you can't do this because this sequence is immutable. Rationale: set holds an ordered sequence, usually in a binary tree. If you were allowed to modify it, you could corrupt the container and the program would crash.

Btw, it still can happen in some situations. You can change your code to this: test. Erase(30); Or use ArunSaha's (+1) code for more complicated criteria.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions