stl list erase

P

Paras

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;

}
}
 
E

ES Kim

Paras said:
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;
}
 
N

Naren

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++;
}
}
 
J

John Harrison

ES Kim said:
Paras said:
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;
}

john
 
C

Chris Theis

Naren said:
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
 
G

Gavin Deane

Paras said:
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
 
R

Rolf Magnus

Gavin said:
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.
 
R

Ron Natalie

Rob Williscroft said:
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...
 
R

Rob Williscroft

llewelly wrote in
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.
 
G

Gavin Deane

Rob Williscroft said:
llewelly wrote in
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
 
K

Kevin Goodsell

Gavin said:
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
 
G

Gavin Deane

Kevin Goodsell said:
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
 
A

Alan Chen

Naren said:
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.
 
A

Alan Chen

Naren said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top