I was trying to use remove_if template for a map container but I am getting compiler error for the template argument. I am unable to understand why.
int main()
{
map<const int, int> intmap;
intmap[1] = 1;
intmap[2] = 2;
intmap[3] = 3;
intmap[4] = 4;
auto isOdd = [&](pair<const int, int> it)->bool
{ return static_cast<bool>(it.second % 2); };
isOdd(*(intmap.begin()));
remove_if(intmap.begin(), intmap.end(), isOdd);
}
This remove_if is throwing compiler errors. Any suggestion to fix it?
Error message is
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\utility(260) : error C2166: l-value specifies const object
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\utility(259) : while compiling class template member function
'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
with
[
_Ty1=const int,
_Ty2=int
]
maperaseif.cpp(29) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
with
[
_Ty1=const int,
_Ty2=int
]
remove_if
works by scanning the elements and once an element is to be removed, it remembers the "gap" that will leave (keeping an iterator pointing thereto) while advancing another iterator to find the next element to retain... it then starts copying or moving elements from the latter position to the former until it reaches end()
.
That doesn't work for map
, because you can't overwrite the pair<key,value>
elements wholesale: the key values aren't allowed to be modified or the sorted-order invariant the implementation needs could be invalidated.
So, you'll need to abandon remove_if
. You could use a normal loop, being careful to save the iterator-to-next-element rather than attempting to advance from a just-erased iterator. Lots of other questions about how to erase elements from a map while iterating, e.g. here....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With