473,587 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set<>::erase(it erator) question

Usually STL containers have something like "iterator
container<>::er ase(iterator)" which erases the element the iterator points
to and returns another valid iterator. Set does not have this, set::erase()
returns void and "invalidate s" the iterator.

Now I have the following code:

while(!some_set .empty()) {
for(it = some_set.begin( ); it != some_set.end(); it++)
{
...
if (something == true) some_set.erase( it);
...
}
}

And it always worked (using GCC3.2). Now running the same program using the
same test data on another machine (GCC3.3) it is caught in an endless loop.
What could be going on here?

Jul 19 '05 #1
9 10885
WW
Harald Grossauer wrote:
Usually STL containers have something like "iterator
container<>::er ase(iterator)" which erases the element the iterator
points to and returns another valid iterator. Set does not have this,
set::erase() returns void and "invalidate s" the iterator.

[SNIP]

Get Effective STL from Scott Meyers.

// This is not necessary: while(!some_set .empty()) {
for(it = some_set.begin( ); it != some_set.end(); ) // Change
{
...
if (something == true) some_set.erase( it++); // Change
else ++it; // Change
...
}

You need to get the valid iterator *before* the erase runs.

--
WW aka Attila
Jul 19 '05 #2

"Harald Grossauer" <ha************ **@uibk.ac.at> wrote in message news:3f******@s ia.uibk.ac.at.. .
Usually STL containers have something like "iterator
container<>::er ase(iterator)" which erases the element the iterator points
to and returns another valid iterator.
That's true for sequences.
Set does not have this, set::erase() returns void and "invalidate s" the iterator.
That's the way associative containers are, unfortuantely. However, the nice thing
is that they only invalidate the erased iterator.
for(it = some_set.begin( ); it != some_set.end(); it++)
{
...
if (something == true) some_set.erase( it);
...
}

You have two chocies:
1. remember the next iterator somewhere
2. use post increment to accomplish the same thing

next_it = it+1;
if(something == true) some_set.erase( it);
it = next_it;

or
if(something == true) some_set.erase( it++);
else ++it;
Jul 19 '05 #3
WW wrote in <bk**********@p hys-news1.kolumbus. fi>:
Harald Grossauer wrote:
Usually STL containers have something like "iterator
container<>::er ase(iterator)" which erases the element the iterator
points to and returns another valid iterator. Set does not have this,
set::erase() returns void and "invalidate s" the iterator. [SNIP]

Get Effective STL from Scott Meyers.

// This is not necessary: while(!some_set .empty()) {
for(it = some_set.begin( ); it != some_set.end(); ) // Change
{
...
if (something == true) some_set.erase( it++); // Change
else ++it; // Change
...
}

You need to get the valid iterator *before* the erase runs.


if i'm not mistaken erase returns the next valid iterator, so:
for(it = some_set.begin( ); it != some_set.end(); ) // Change
{
... if (something == true)
it = some_set.erase( it++);
... }


should do the trick?

--
Groeten keanu,
Jabber ID: knu[at]amessage[dot]de
GPG Key-ID: 0x3246DE81
Jul 19 '05 #4

"keanu" <us****@keanu.b e> wrote in message news:H0******** *************@p hobos.telenet-ops.be...
if i'm not mistaken erase returns the next valid iterator, so:
Yoiu are mistaken. Did you even read the original post? The erase on
associative containers do not, unfortunately, return anything.
for(it = some_set.begin( ); it != some_set.end(); ) // Change
{
...

if (something == true)
it = some_set.erase( it++);
...
}


That won't even work on containers where erase DOES return something. It's
undefined behavior in that case. You can do on sequences

if(something)
it = some_sequence.e rase(it);
else ++it;

or on associative containers
if(something)
some_set(it++);
else ++it;
Jul 19 '05 #5
keanu wrote:

if i'm not mistaken erase returns the next valid iterator, so:


Not for associative containers. That's sort of the point of the thread.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Ron Natalie wrote:
for(it = some_set.begin( ); it != some_set.end(); ) // Change
{
...


if (something == true)
it = some_set.erase( it++);
...
}

That won't even work on containers where erase DOES return something. It's
undefined behavior in that case.


Why is it undefined behavior? The increment is 'finished' before the
function call (sequence point immediately before a function call) and
the assignment occurs after, right?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7
> That won't even work on containers where erase DOES return something.
It's
undefined behavior in that case. You can do on sequences

if(something)
it = some_sequence.e rase(it);
else ++it;


this is what i meant, the ++ was a left over from the copy paste + no sleep

for some reason i thought erase returns the next iterator for every
container

Jul 19 '05 #8

"Kevin Goodsell" <us************ *********@never box.com> wrote in message news:MI******** ******@newsread 4.news.pas.eart hlink.net...
it = some_set.erase( it++);

Why is it undefined behavior? The increment is 'finished' before the
function call (sequence point immediately before a function call) and
the assignment occurs after, right?


Yeah, I guess you're right. I was thinking that the side effect could be delayed
but there is an inherent sequence point after the function args are evaluated and
again on return.
Jul 19 '05 #9
Ron Natalie wrote:

You have two chocies:
1. remember the next iterator somewhere
2. use post increment to accomplish the same thing

next_it = it+1;
if(something == true) some_set.erase( it);
it = next_it;
That's how I did it then. I just thought there might be a "nicer" version
without introducing another "dummy iterator".

or
if(something == true) some_set.erase( it++);
else ++it;


Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3670
by: Steve Edwards | last post by:
Hi, 1) I'm making a container for string pointers... set<string*> strSet; strSet.insert(...); // insert some strings ....
22
2683
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least the following methods must be implemented: insert(), find(), begin(), end(), erase(), size(), operator<(), and at least the forward iterator. ...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.