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? 9 10885
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
"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;
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
"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;
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.
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.
> 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
"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.
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; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steve Edwards |
last post by:
Hi,
1)
I'm making a container for string pointers...
set<string*> strSet;
strSet.insert(...); // insert some strings
....
|
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.
...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |