473,386 Members | 2,078 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,386 software developers and data experts.

set<>::erase(iterator) question

Usually STL containers have something like "iterator
container<>::erase(iterator)" which erases the element the iterator points
to and returns another valid iterator. Set does not have this, set::erase()
returns void and "invalidates" 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 10874
WW
Harald Grossauer wrote:
Usually STL containers have something like "iterator
container<>::erase(iterator)" which erases the element the iterator
points to and returns another valid iterator. Set does not have this,
set::erase() returns void and "invalidates" 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******@sia.uibk.ac.at...
Usually STL containers have something like "iterator
container<>::erase(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 "invalidates" 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**********@phys-news1.kolumbus.fi>:
Harald Grossauer wrote:
Usually STL containers have something like "iterator
container<>::erase(iterator)" which erases the element the iterator
points to and returns another valid iterator. Set does not have this,
set::erase() returns void and "invalidates" 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.be> wrote in message news:H0*********************@phobos.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.erase(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.erase(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*********************@neverbox.com> wrote in message news:MI**************@newsread4.news.pas.earthlink .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
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.