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 7 of 14 Topic 9541 of 9828
Post > Topic >>

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

by Greg Herlihy <greghe@[EMAIL PROTECTED] > Apr 25, 2008 at 10:14 AM

On Apr 24, 2:43 pm, Chris Uzdavinis <cuz...@[EMAIL PROTECTED]
> wrote:
>
> The only code you have that is really unique to this problem is the
> test for whether the element should be erased or not.  Factor that out
> into its own inlined function object to be a simple predicate:
>
>   struct is_odd
>   {
>     bool operator()(int num) const { return num & 1; }
>   };
>
> Then rewrite delete_odd using standard library function remove_if
> which uses your predicate:
>
>   #include <algorithm>
>
>   void delete_odd(int_list_t & L)
>   {
>     // move the even elements to the front of the list
>     int_list_t::iterator logical_end =
>       std::remove_if(L.begin(), L.end(), is_odd());
>
>     // get rid of odd values that are now at the end of the list.
>     L.erase(logical_end, L.end());
>   }
>
> Note that remove_if doesn't really remove anything, but bubbles the
> keepers up to the front of the sequence, and puts the trash at the
> end, returning the logical end of the sequence.  Therefore, we erase
> the trash at the end from the logical end to the physical end with the
> call to erase on the list.

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). Since the appeal of working with stable iterators is often
one of the primary reasons for storing values in a std::list in the
first place - it would make sense to avoid calling those routines in
<algorithm> that invalidate iterators - and to call the comparable
std::list member functions instead.

So, instead of calling std::remove_if() in this case - and thereby
potentially invalidating the list's iterators - a better idea would be
to call std::list's own remove_if() member function and thereby
preserve the validity of any iterators that remain in the list after
the call:

     void delete_odd(int_list_t& L)
     {
         L.remove_if(isodd);
     }

This revised implementation of delete_odd() also has an advantage in
compactness - being only one-third the size of the original.

Greg



-- 
      [ 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 Thu Jul 24 15:45:58 CDT 2008.