473,569 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*>::itera tor 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 20482
"Paras" <pa******@netsc ape.net> wrote in message
news:3F******** ****@netscape.n et...

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*>::itera tor 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*>::itera tor 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*>::itera tor si1 = _ls.begin();
while(si1 !=_ls.end()) {

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

else
{
si1++;
}
}

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

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*>::itera tor 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.k r> wrote in message
news:bh******** **@news1.kornet .net...
"Paras" <pa******@netsc ape.net> wrote in message
news:3F******** ****@netscape.n et...

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*>::itera tor 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.inter net.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******@netsc ape.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*>::itera tor 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.RE MOVE.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******** ****@Zorthluthi k.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.RE MOVE.co.uk> wrote in message news:<Xn******* *************** ************@19 5.129.110.201>. ..
llewelly wrote in news:86******** ****@Zorthluthi k.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

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

Similar topics

2
9953
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
2855
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
12
1941
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> #include<iostream> int main(){
9
3295
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
4933
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 new, logical end pointer, so that the following myList::iterator end = myListObj.remove(myInt); myListObj.erase(end, myListObj.end()); is...
25
3852
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 is important that I iterate through the list backwards, because the items in it have to be processed in reverse order before erasing. However,...
13
6364
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 erase all elements from the list. my progam crashes. I delete element by using a iterator.
3
2119
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 we have an integer list p, say. For each element x in p, we want to repace x with f(x) to get a new, possibly larger, integer list. Note
6
1699
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 element the iterator points to... Basically, a list element has pointers back and forth, so I shouldn't need the list itself, but I don't see any...
7
2337
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
0
7703
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, 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...
0
7618
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...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.