Thank you all for the informative replies.
I am well aware of the remove_if and functors, but there are reasons I
haven't used them, and why I did not delete items right away.
Suppose that the condition we remove an item is not as simple as is-
odd, but require some other checking from other structures and
variables all present within the delete_odd().
In real life problem, there would be something like:
// please note that process_items only reads/erase items
void process_items(int_list_t &L)
{
//some variables
//some reference and iterators to other variables and data
structures...
for (...iterator to walk in L...)
{
// do some complex checking, from L's members and other variables
present here
// ..
// should we decided to delete the item once we are done with the
loop?
// we don't delete immediatly because we might need to reference
previous items
// that's why we wait till end of loop and delete all "marked-for-
deletion" items
}
// now all processing is done, we clear all marked for deletion items
for (...iterator of iterator...)
{
erase(iterator of item to be deleted);
}
To summarize, what would you do if these conditions apply:
1. the remove_if is so complex and requires a certain context to
decide, thus we cannot have a functor with lots of parameters just to
provide it with the required context.
2. items are not to be erased on the spot, we should wait for a
certain operation to end, then delete them, because those items might
be needed and referenced and read while process is not done yet.
Now, is my original post/code the best way to meet these conditions?
2)
Would my code break, if we changed a simple line in delete_odd(), as:
From:
int_list_t::iterator it;
for (it=L.begin();it!=L.end();++it)
To:
for (int_list_t::iterator it=L.begin();it!=L.end();++it)
Regards,
Elias
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|