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

stl list erase


Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
for (si1=_ls.begin(); si1!=_ls.end();++si1) {

if ( (*si1)->check() ) {
a =(*si);
_ls.erase(si1);
/* Now this function will point pint si1 to next element. (no
invalidate) Now how to still continue with for loop which will be a
problem now ??? ..
*/

delete a;

}
}
Jul 19 '05 #1
13 20468
"Paras" <pa******@netscape.net> wrote in message
news:3F************@netscape.net...

Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
for (si1=_ls.begin(); si1!=_ls.end();++si1) {

if ( (*si1)->check() ) {
a =(*si);
_ls.erase(si1);
/* Now this function will point pint si1 to next element. (no
invalidate) Now how to still continue with for loop which will be a
problem now ??? ..
*/

delete a;

}
}

list<A*> _ls;
A* a;
list<A*>::iterator si1 = _ls.begin();
while (si1 != _ls.end())
{
if ((*si1)->check())
{
a = *si1;
// si1 points to the next element after erase()
si1 = _ls.erase(si1);
delete a;

}
else
++si1;
}

--
ES Kim
Jul 19 '05 #2
Hello,

erase returns a iterator to first element remaining beyond any elements
removed.so use this

I like *while* so i have changed it to while sorry..;-)

Hope this helps.

//Code

list<A*> _ls;
A * a;
list<A*>::iterator si1 = _ls.begin();
while(si1 !=_ls.end()) {

if ( (*si1)->check() ) {
a =(*si);
si1 = _ls.erase(si1);
delete a;
}

else
{
si1++;
}
}

"Paras" <pa******@netscape.net> wrote in message
news:3F************@netscape.net...

Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
for (si1=_ls.begin(); si1!=_ls.end();++si1) {

if ( (*si1)->check() ) {
a =(*si);
_ls.erase(si1);
/* Now this function will point pint si1 to next element. (no
invalidate) Now how to still continue with for loop which will be a
problem now ??? ..
*/

delete a;

}
}

Jul 19 '05 #3

"ES Kim" <es***@svd.co.kr> wrote in message
news:bh**********@news1.kornet.net...
"Paras" <pa******@netscape.net> wrote in message
news:3F************@netscape.net...

Hi
What is the correct way to delete an element from STL list while
iterating through the list

[snip]

list<A*> _ls;
A* a;
list<A*>::iterator si1 = _ls.begin();
while (si1 != _ls.end())
{
if ((*si1)->check())
{
a = *si1;
// si1 points to the next element after erase()
si1 = _ls.erase(si1);
delete a;

No good reason for the a varaible

delete *si1;
si1 = _ls.erase(si1);
}
else
++si1;
}

--
ES Kim


john
Jul 19 '05 #4

"Naren" <na*************@in.bosch.com> wrote in message
news:bh**********@ns2.fe.internet.bosch.com...
Hello,

erase returns a iterator to first element remaining beyond any elements
removed.so use this

I like *while* so i have changed it to while sorry..;-)

Hope this helps.

//Code
[SNIP]
else
{
si1++;
In this case I'd recommend to use ++si1; instead because it might be more
efficient. Anyway there's nothing to lose if you use prefix instead of
postfix notation in a case like this.
}
}


Chris
Jul 19 '05 #5
Paras <pa******@netscape.net> wrote in message news:<3F************@netscape.net>...
Hi
What is the correct way to delete an element from STL list while
iterating through the list
list<A*> _ls;
A * a;
list<A*>::iterator si1;
for (si1=_ls.begin(); si1!=_ls.end();++si1) {

if ( (*si1)->check() ) {
a =(*si);
_ls.erase(si1);
/* Now this function will point pint si1 to next element. (no
invalidate) Now how to still continue with for loop which will be a
problem now ??? ..
*/

delete a;

}
}


Other people appear to have answered your question, but just to point
out:

Names beginning with an underscore are reserved for the implementation
in the global namespace so, depending on context, _ls might not be the
best name for your list.

GJD
Jul 19 '05 #6
Gavin Deane wrote:
Names beginning with an underscore are reserved for the implementation
in the global namespace


I thought this only applied if that undersore was followed by an
uppercase letter.

Jul 19 '05 #7

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message

If I remember/understand correctly, those names and those with two
underscores are reserved anywhere, i.e. so the implementor can use
them as macro's.
And also to set off the decorations from the rest of the identifier (the
double-underscore prohibition isn't just for leading undersdcores but
anywhere in the identifier).
Identifiers with a single leading underscore are reserved only in
the global namespace.


NO. Indentifiers with leading underscore and a captial letter are not
permitted anywhere. If you look at a lot of the template library you'll
see many implementations use these symbols as their template variables,
etc...to keep things from colliding with possible user macro definitions.

It is _ followed by non-uppercase letters that is reserved ONLY in the
global namespace. These are typically used for certain implementation
defined global functions etc...

Jul 19 '05 #8
llewelly wrote in news:86************@Zorthluthik.local.bar:
If I remember/understand correctly, those names and those with two
underscores are reserved anywhere, i.e. so the implementor can use
them as macro's.

[snip]

Two *consecutive* underscores; i.e. this_is_okay this__is_not


Yes, It's what I meant but not what I wrote, Thanks.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #9
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.201>...
llewelly wrote in news:86************@Zorthluthik.local.bar:
If I remember/understand correctly, those names and those with two
underscores are reserved anywhere, i.e. so the implementor can use
them as macro's.

[snip]

Two *consecutive* underscores; i.e. this_is_okay this__is_not


Yes, It's what I meant but not what I wrote, Thanks.

Rob.


I guess the level of uncertainty here lends weight to the argument
that it's simpler just to avoid leading underscores altogether (which
is my preference anyway). I checked the standard before my original
post in this thread so I'm fairly sure it was right, but the fact that
I felt I needed to supports that argument.

GJD
Jul 19 '05 #10
Gavin Deane wrote:

I guess the level of uncertainty here lends weight to the argument
that it's simpler just to avoid leading underscores altogether (which
is my preference anyway).


I certainly agree with that. I've tried, but never been able to
determine for sure what the exact rules are.

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

Jul 19 '05 #11
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<3f441c01@shknews01>...
Gavin Deane wrote:

I guess the level of uncertainty here lends weight to the argument
that it's simpler just to avoid leading underscores altogether (which
is my preference anyway).


I certainly agree with that. I've tried, but never been able to
determine for sure what the exact rules are.


The rules are in 17.4.3.1 in the standard if you have a copy. Well
they're there whether you have a copy or not, but it will be harder
for you to look them up if you haven't ;-)

And as I said, just because I _can_ look them up doesn't mean I want
to every time I choose a variable name, so I steer clear of leading
underscores altogether.

GJD
Jul 19 '05 #12
"Naren" <na*************@in.bosch.com> wrote in message news:<bh**********@ns2.fe.internet.bosch.com>...
Hello,

erase returns a iterator to first element remaining beyond any elements
removed.so use this

I like *while* so i have changed it to while sorry..;-)

Hope this helps.

//Code

list<A*> _ls;
A * a;
list<A*>::iterator si1 = _ls.begin();
while(si1 !=_ls.end()) {

if ( (*si1)->check() ) {
a =(*si);
si1 = _ls.erase(si1);
delete a;
}
else
{
si1++;
}
}


There's a bug in this code. If the second to last element is deleted,
the last element will never be checked for deletion.
Jul 19 '05 #13
"Naren" <na*************@in.bosch.com> wrote in message news:<bh**********@ns2.fe.internet.bosch.com>...
Hello,

erase returns a iterator to first element remaining beyond any elements
removed.so use this

I like *while* so i have changed it to while sorry..;-)

Hope this helps.

//Code

list<A*> _ls;
A * a;
list<A*>::iterator si1 = _ls.begin();
while(si1 !=_ls.end()) {

if ( (*si1)->check() ) {
a =(*si);
si1 = _ls.erase(si1);
delete a;
}

else
{
si1++;
}
}


Nevermind, there is a bug in my brain ... never post w/o morning coffee...
Jul 19 '05 #14

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

Similar topics

2
by: s | last post by:
Here is a snippet of my code: <code> list< MyClass * >outgoing_pool; MyClass* GetWaitingObject(string freq) { MyClass *temp_ptr = NULL; list<MyClass*>::iterator i;
8
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
12
by: Christoff Pale | last post by:
Hi, suppose I have a list of strings; I have to iterate of the strings and delete certain entries. I and not sure how to do this? for example: #include<list> #include<string>...
9
by: Gunnar G | last post by:
Hello, I just want to make sure I got it right. I have this code list<int> L; L.push_back(1); L.push_back(2); L.push_back(3); L.push_back(4); list<int>::iterator iter=L.begin()+1;
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
25
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
13
by: mahajan.vibhor | last post by:
I have a list of pointers. e.g A* a = new A(); // A is a class stl::list<A*list_a; I am inserting object of class in the after allocating memeory thru new operator. But when i want to...
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
6
by: alon | last post by:
I got the following situation: I have a few lists of integers, and I have an iterator pointing to one of the elements. I don't have a pointer to the list itself ! All I want is to remove the...
7
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...
0
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...

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.