how to erase entries in a map on-the-fly ?
Hi All,
I think that it is not legitimate to erase entries in a map in the following way:
Code:
MyMap temp;
//fill in temp here
MyMap::iterator it; //MyMap is a typedef of STL map
for(it = temp.begin(); it != temp.end(); it++)
{
if( condition )
{
temp.erase(it->first); //erase by key
}
}
The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.
Thanks.
Re: how to erase entries in a map on-the-fly ?
Quote:
Originally Posted by aryan1
The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.
I shall paraphrase the example that I often refer to, from Josuttis' The C++ Standard Library:
Code:
for (it = temp.begin(); it != temp.end();)
{
if (condition)
{
temp.erase(it++);
}
else
{
++it;
}
}
Re: how to erase entries in a map on-the-fly ?
Quote:
Originally Posted by
aryan1
Hi All,
I think that it is not legitimate to erase entries in a map in the following way:
Code:
MyMap temp;
//fill in temp here
MyMap::iterator it; //MyMap is a typedef of STL map
for(it = temp.begin(); it != temp.end(); it++)
{
if( condition )
{
temp.erase(it->first); //erase by key
}
}
The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.
Thanks.
Code:
#include <algorithm>
bool SomeCondition(const MyMap::value_type& v)
{
//...return true if v is to be erased, false otherwise
}
temp.erase(std::remove_if(temp.begin(), temp.end(), SomeCondition),
temp.end());
Regards,
Paul McKenzie
Re: how to erase entries in a map on-the-fly ?
Paul McKenzie : it is a std::map, so std::remove_if is not appropriate.
Re: how to erase entries in a map on-the-fly ?
Quote:
Originally Posted by
laserlight
Paul McKenzie : it is a std::map, so std::remove_if is not appropriate.
Yes, I was going to say the same thing. 95% sure it compiles, and 95% sure it messes up your map.
Re: how to erase entries in a map on-the-fly ?
Some people will show you this form:
Code:
for (it = temp.begin(); it != temp.end();)
{
if (condition)
{
it = temp.erase(it);
}
else
{
++it;
}
}
While this is more straight forward and easier to understand, it does not conform to the standard. erase does not actually return an iterator.
Re: how to erase entries in a map on-the-fly ?
Quote:
Originally Posted by
monarch_dodra
Yes, I was going to say the same thing. 95% sure it compiles, and 95% sure it messes up your map.
Yes, it won't work. Only forward iterators should be used, so associative containers seem to be off-limits.
Regards,
Paul McKenzie
Re: how to erase entries in a map on-the-fly ?
To quote "Effective STL"
Use this for std::vector, std::string & std::deque
Code:
for (it = temp.begin(); it != temp.end();)
{
if (condition)
{
it = temp.erase(it);
}
else
{
++it;
}
}
For others use
Code:
for (it = temp.begin(); it != temp.end();)
{
if (condition)
{
temp.erase(it++);
}
else
{
++it;
}
}
For std::list use either.