By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

how can I loop a std::list ?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.