Can someone help me out here?
Compiling this code:
void test()
{
std::set<int> test;
test.insert(42);
test.erase(std::remove(test.begin(), test.end(), 30), test.end()); // <- Line 33
}
Is generating the following error when compiling:
$ make
g++ -c -Wall -pedantic-errors -Wextra -Wunused -Werror a_star.cpp
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h: In function `_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = std::_Rb_tree_const_iterator<int>, _Tp = int]':
a_star.cpp:33: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h:779: error: assignment of read-only location `__result.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = int]()'
make: *** [a_star.o] Error 1
In
std::set, the elements are not modifiable. So, thestd::set::iteratoris also unmodifiable. From this tutorial, section 27.3.2.1:Hence, the
erase-removeidiom cannot be applied as is. You have to write aforloop, and use the member functionstd::set::eraseinside it. See this question and this accepted answer and another answer for exact details, but in short, the loop is like the following