By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

delete an entry from map

P: n/a
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.

Feb 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
we*****@yahoo.com wrote:
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
Feb 23 '07 #2

P: n/a
On Feb 23, 5:28 pm, John Harrison <john_androni...@hotmail.comwrote:
wenm...@yahoo.com wrote:
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
No. Thank for pointing out the pre-increment of iterator. I almost
forgot about it.

Feb 23 '07 #3

P: n/a
On Fri, 23 Feb 2007 22:28:02 GMT in comp.lang.c++, John Harrison
<jo*************@hotmail.comwrote,
>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().

Feb 24 '07 #4

P: n/a
David Harmon wrote:
On Fri, 23 Feb 2007 22:28:02 GMT in comp.lang.c++, John Harrison
<jo*************@hotmail.comwrote,
>>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().
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
Feb 24 '07 #5

P: n/a
John Harrison wrote:
David Harmon wrote:
>On Fri, 23 Feb 2007 22:28:02 GMT in comp.lang.c++, John Harrison
<jo*************@hotmail.comwrote,
>>>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().

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
Feb 24 '07 #6

P: n/a
On Sat, 24 Feb 2007 07:13:59 GMT in comp.lang.c++, John Harrison
<jo*************@hotmail.comwrote,
>Returned iterator from map::erase is not required by the standard so
this code is non-standard. Apparently this is a somewhat controverial topic.
Darn it, I can't keep it all straight. I was just looking at the C++0x
draft, and it's gonna be standard eventually.
http://www.open-std.org/jtc1/sc22/wg...2006/n2135.pdf

Feb 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.