473,698 Members | 2,508 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
13 20486
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************ *********@never box.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.inte rnet.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*>::itera tor 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.inte rnet.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*>::itera tor 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
9959
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
2861
by: ma740988 | last post by:
Consider: # include <iostream> using std::cout; using std::cin; using std::endl; # include <list> using std::list;
12
1953
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
3297
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
4942
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 possible and removes the "invalid" items at the end of the list.
25
3865
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, there does not appear to be an std::list::erase() method defined for reverse iterators.
13
6427
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
2126
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
1702
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 function that does the work. Is there a real problem to do this? Is it just a coincidence that no body...
7
2345
by: TBass | last post by:
So I have a class: class Client { unsigned int ClientID; .... }; class MyListenSocket
0
8685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.