473,473 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can I loop a std::list ?

Hi guys,

i have a list which have some objects in it

and I want to find a specific one and delete that,I thought that it could
be done with iterator like
//=======fake code==============
for(iteratorA=list.begin();iteratorA<list.end();++ iteratorA)

//======================

but I failed,is there any way loop through the list (without pop
anything,list stays same)?

Thanks a lot!!
Jul 22 '05 #1
6 5538
"Joseph" <wu*******@hotmail.com> wrote in message
for(iteratorA=list.begin();iteratorA<list.end();++ iteratorA)


There is no operator< for pretty much all iterators. It would be too
expensive to implement for a list. Use != instead.

for(iteratorA=list.begin();iteratorA!=list.end();+ +iteratorA) {
cout << *iteratorA;
}

As for removing an element from a list, you can use the member functions
list::remove or list::remove_if.

Please be aware that the following does not work, and can you tell why?

for(iteratorA=list.begin();iteratorA!=list.end();+ +iteratorA) {
if (*iteratorA == 3) list.erase(iteratorA);
}
Jul 22 '05 #2
Iterators are a good option. Probably you have made some mistake in your
code.

What's the iterator's object declaration?
What's the error you get?

Joseph escribió:
Hi guys,

i have a list which have some objects in it

and I want to find a specific one and delete that,I thought that it could
be done with iterator like
//=======fake code==============
for(iteratorA=list.begin();iteratorA<list.end();++ iteratorA)

//======================

but I failed,is there any way loop through the list (without pop
anything,list stays same)?

Thanks a lot!!

Jul 22 '05 #3
Carlos Martinez Garcia <cm****@tid.es> wrote in
news:cf*********@news.hi.inet:
Iterators are a good option. Probably you have made some mistake in
your code.

What's the iterator's object declaration?
What's the error you get?

Joseph escribi?
Hi guys,

i have a list which have some objects in it

and I want to find a specific one and delete that,I thought that it
could be done with iterator like
//=======fake code==============
for(iteratorA=list.begin();iteratorA<list.end();++ iteratorA)

//======================

but I failed,is there any way loop through the list (without pop
anything,list stays same)?

Thanks a lot!!


Hi Carlos,

the error is exactlly like Siemel said(no "<" operator),so ,I would use !=
instead.

cheers!
Jul 22 '05 #4
Siemel Naran wrote:
"Joseph" <wu*******@hotmail.com> wrote in message
for(iteratorA=list.begin();iteratorA<list.end();++ iteratorA)


There is no operator< for pretty much all iterators. It would be too
expensive to implement for a list. Use != instead.

for(iteratorA=list.begin();iteratorA!=list.end();+ +iteratorA) {
cout << *iteratorA;
}

As for removing an element from a list, you can use the member functions
list::remove or list::remove_if.

Please be aware that the following does not work, and can you tell why?

for(iteratorA=list.begin();iteratorA!=list.end();+ +iteratorA) {
if (*iteratorA == 3) list.erase(iteratorA);
}


Of course, the iterator is invalid after the erase. It points to a delete
item.

by
David

Jul 22 '05 #5
> and I want to find a specific one and delete that,I thought that it could
be done with iterator like


I have done something like that and here is the idea!
I make a copy of the list with all the items to be deleted!
Since there is alot of selections to be considered I found that
this way give me a good readibility!
The destructor of the object removes and deletes it self from the static
main list!
Ofcourse LIST is a typedef of my objects!
{
LIST l;
std::copy_if(List.begin(),List.end(),std::back_ins erter(l),pred1);
std::copy_if(List.begin(),List.end(),std::back_ins erter(l),pred2);
for(LIST::iterator i=l.begin();i!=l.end();i++)
delete (*i);
}

//jota
Jul 22 '05 #6
"David Ecker" <da***@familie-ecker.net> wrote in message news:D4GSc.2784
Siemel Naran wrote:

Please be aware that the following does not work, and can you tell why?

for(iteratorA=list.begin();iteratorA!=list.end();+ +iteratorA) {
if (*iteratorA == 3) list.erase(iteratorA);
}


Of course, the iterator is invalid after the erase. It points to a delete
item.


Right. So do you know how to fix it (assuming you don't want to use
list::remove or are implementing this function)?
Jul 22 '05 #7

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

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
2
by: raven.mp4 | last post by:
Hi, I have a little question about std::list iterators. Take a look at this piece of code: list<Type*>::iterator it = myList.begin(); Type *p; for(it; it != myList.end(); ++it) {
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
1
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...
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,...
1
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...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.