how can I loop a std::list ?

J

Joseph

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!!
 
S

Siemel Naran

Joseph said:
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);
}
 
C

Carlos Martinez Garcia

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ó:
 
J

Joseph

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 Carlos,

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

cheers!
 
D

David Ecker

Siemel said:
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
 
J

jota

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_inserter(l),pred1);
std::copy_if(List.begin(),List.end(),std::back_inserter(l),pred2);
for(LIST::iterator i=l.begin();i!=l.end();i++)
delete (*i);
}

//jota
 
S

Siemel Naran

David Ecker said:
Siemel Naran wrote:

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)?
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top