

|
 |
| Programming > C++ Moderated > Re: Deleting it... |
|
| << 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:
|
lallous <lallous@[EMAI |
2008-04-24 01:04:11 |
|
"Paul M. Dubuc" |
2008-04-24 15:19:20 |
|
Ron Natalie <ron@[EMAI |
2008-04-24 15:43:19 |
|
Chris Uzdavinis <cuzda |
2008-04-24 15:43:20 |
|
Alex Shulgin <alex.shu |
2008-04-25 03:42:15 |
|
Chris Uzdavinis <cuzda |
2008-04-25 10:14:33 |
|
Greg Herlihy <greghe@[ |
2008-04-25 10:14:41 |
|
Tomislav Petrovic <t.p |
2008-04-25 10:45:35 |
|
Chris Uzdavinis <cuzda |
2008-04-25 15:50:25 |
|
Carl Barron <cbarron41 |
2008-04-26 03:34:23 |
|
lallous <lallous@[EMAI |
2008-04-26 04:44:01 |
|
Greg Herlihy <greghe@[ |
2008-04-26 08:43:46 |
|
Carl Barron <cbarron41 |
2008-04-26 16:32:32 |
|
Carl Barron <cbarron41 |
2008-04-26 16:32:41 |
|
Post A Reply:

|
|
|
|