delete an entry from map

W

wenmang

Hi,
I am wondering what the safe way to delete a map entry through its
iterator. There is a example from the book of Nicolai Josuttis:
for(map::iterator itr=myMap.begin();itr !=myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
itr++;
}

is there other way to do so or this is the only way?
thx.
 
J

John Harrison

Hi,
I am wondering what the safe way to delete a map entry through its
iterator. There is a example from the book of Nicolai Josuttis:
for(map::iterator itr=myMap.begin();itr !=myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
itr++;
}

is there other way to do so or this is the only way?
thx.

This is a very slight improvment.

for(map::iterator itr=myMap.begin();itr !=myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
++itr;
}

It definitely not the only way, but it is the best way as far as I know.
Do you have a problem with it?

john
 
W

wenmang

This is a very slight improvment.

for(map::iterator itr=myMap.begin();itr !=myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
++itr;

}

It definitely not the only way, but it is the best way as far as I know.
Do you have a problem with it?

john

No. Thank for pointing out the pre-increment of iterator. I almost
forgot about it.
 
D

David Harmon

On Fri, 23 Feb 2007 22:28:02 GMT in comp.lang.c++, John Harrison
This is a very slight improvment.

for(map::iterator itr=myMap.begin();itr !=myMap.end())
{
if(itr->value == something)
myMap.erase(itr++);
else
++itr;
}

In all optimization, YMMV. But I think this is another slight
improvement. Also some typos corrected.

for(map::iterator itr=myMap.begin(); itr !=myMap.end(); )
{
if(itr->first == something)
itr = myMap.erase(itr);
else
++itr;
}

However, some implementations may not support the return iterator from
map::erase().
 
J

John Harrison

David said:
On Fri, 23 Feb 2007 22:28:02 GMT in comp.lang.c++, John Harrison



In all optimization, YMMV. But I think this is another slight
improvement. Also some typos corrected.

for(map::iterator itr=myMap.begin(); itr !=myMap.end(); )
{
if(itr->first == something)
itr = myMap.erase(itr);
else
++itr;
}

However, some implementations may not support the return iterator from
map::erase().

Returned iterator from map::erase is not required by the standard so
this code is non-standard. Apparently this is a somewhat controverial topic.

john
 
K

Kai-Uwe Bux

John said:
Returned iterator from map::erase is not required by the standard so
this code is non-standard. Apparently this is a somewhat controverial
topic.

It is not _yet_ required by the standard: The current draft N2134 for C++0X
provides that erase() returns an iterator for all containers.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top