Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Deleting it...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 14 Topic 9541 of 9807
Post > Topic >>

Re: Deleting items from an std::list , is this code correct?

by Carl Barron <cbarron413@[EMAIL PROTECTED] > Apr 26, 2008 at 03:34 AM

In article
<6eaa63d2-cb20-432a-a500-dc610e9f877b@[EMAIL PROTECTED]
>,
Greg Herlihy <greghe@[EMAIL PROTECTED]
> wrote:

> Calling std::remove_if() on a std::list can invalidate the list's
> iterators (meaning that after the call to remove_if() - each iterator
> in the list might no longer reference the same value bas it did before
> the call) 

    True the iterators of the 'removed' items are invalid, but the
iterators to the remaining ones are valid.

    bool is_even(int);
    std::list<int>  foo;
    for(int i=0;i!=10;++i) foo.push_back(i); // list = {0,1,2,3,4,5,..9{
    std::list<int>::iterator last = std::remove_if(foo.begin(),foo.end(),
       is_even);

list is now {1,3,5,7,9,j,k,k,k,k} still 10 entries but only first 5
[last points to j]  are in the sequence.  all iterators in
[foo.begin(),last) are valid.

If you want the garbage [denote as j or k above] then call erase with
two iterators or better still use the list::remove_if() member function
and the items will be physically removed from the list in one pass that
is foo.remove_if(is_even); will make foo.size() == 5 while
std::remove_if(...) will leave foo.size()==10,

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 14 Posts in Topic:
Deleting items from an std::list , is this code correct?
lallous <lallous@[EMAI  2008-04-24 01:04:11 
Re: Deleting items from an std::list , is this code correct?
"Paul M. Dubuc"  2008-04-24 15:19:20 
Re: Deleting items from an std::list , is this code correct?
Ron Natalie <ron@[EMAI  2008-04-24 15:43:19 
Re: Deleting items from an std::list , is this code correct?
Chris Uzdavinis <cuzda  2008-04-24 15:43:20 
Re: Deleting items from an std::list , is this code correct?
Alex Shulgin <alex.shu  2008-04-25 03:42:15 
Re: Deleting items from an std::list , is this code correct?
Chris Uzdavinis <cuzda  2008-04-25 10:14:33 
Re: Deleting items from an std::list , is this code correct?
Greg Herlihy <greghe@[  2008-04-25 10:14:41 
Re: Deleting items from an std::list , is this code correct?
Tomislav Petrovic <t.p  2008-04-25 10:45:35 
Re: Deleting items from an std::list , is this code correct?
Chris Uzdavinis <cuzda  2008-04-25 15:50:25 
Re: Deleting items from an std::list , is this code correct?
Carl Barron <cbarron41  2008-04-26 03:34:23 
Re: Deleting items from an std::list , is this code correct?
lallous <lallous@[EMAI  2008-04-26 04:44:01 
Re: Deleting items from an std::list , is this code correct?
Greg Herlihy <greghe@[  2008-04-26 08:43:46 
Re: Deleting items from an std::list , is this code correct?
Carl Barron <cbarron41  2008-04-26 16:32:32 
Re: Deleting items from an std::list , is this code correct?
Carl Barron <cbarron41  2008-04-26 16:32:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 19 19:46:35 CDT 2008.