473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

delete an entry from map

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
6 10730
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mudcat | last post by:
Howdy, I have a simple combox that I have commands entered into which looks like this: """This is a list with the commands to be run in rotisserie fasion.""" self.comList =...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
10
by: nick_faye | last post by:
Hi guys, i'm still a newbie in using MS Access and in VB programming. I am using DAO connection from my VB to access the entries on my MS Access table. I am having trouble in editting and...
3
by: John Rivers | last post by:
Hello, I think this will apply to alot of web applications: users want the ability to delete a record in table x this record is related to records in other tables and those to others in...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
6
by: dan | last post by:
Hi, I have a table like this: ColA ColB 22 Drink 22 Eat 22 Spill 23 Drink 23 Eat
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
1
by: tvks | last post by:
Dear all, I have an xml file called abc.xml. It is of the following format. <?xml version="1.0" encoding="ISO-8859-1"?> <contacts> <entry uri="sip:vinayg@company.org">...
3
by: Riun | last post by:
when I use the following code: Dim stDocName as String stDocName = "DELETE FROM WHERE =" & txtID DoCmd.RunSQL stDocName the whole row will be deleted from the table. I only want the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.