c++ - Why std::remove takes const version of iterators? -
this question has answer here:
- why std::remove not work std::set? 1 answer
i using visual studio 2013 compile simple code:
std::set<int> a{ 1, 2, 3 }; std::remove(a.begin(), a.end(), 3); i expect can't go wrong surprised. error emits:
error 1 error c3892: '_next' : cannot assign variable const c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm how be? non-const std::set. std::remove() moves element of , seems legal.
in vs 2008, below similar code compiles no error:
std::set<int> a; std::remove(a.begin(), a.end(), 3);
the set non-const set::begin(), set::end() returns const_iterator. can seen on cppreference.
i believe avoid "outsiders" being able swap elements around 1 of set's invariants elements sorted (this std::remove does, moves elements around can call erase on afterwards, see erase-remove idiom)
if want remove element use erase member function.
Comments
Post a Comment