-
April 28th, 2010, 04:06 AM
#1
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.
-
April 28th, 2010, 04:27 AM
#2
Re: how to erase entries in a map on-the-fly ?
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;
}
}
-
April 28th, 2010, 04:28 AM
#3
Re: how to erase entries in a map on-the-fly ?
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
-
April 28th, 2010, 04:29 AM
#4
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.
-
April 28th, 2010, 04:31 AM
#5
Re: how to erase entries in a map on-the-fly ?
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.
-
April 28th, 2010, 04:38 AM
#6
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.
-
April 28th, 2010, 04:39 AM
#7
Re: how to erase entries in a map on-the-fly ?
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
-
April 28th, 2010, 05:00 AM
#8
Re: how to erase entries in a map on-the-fly ?
To quote "Effective STL"
Use this for std::vector, std::string & std:eque
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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|