By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

stl list erase

P: n/a

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
Share this Question
Share on Google+
13 Replies


P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.