In article
<6742faed-a22c-422f-8891-581674443cb3@[EMAIL PROTECTED]
>,
lallous <lallous@[EMAIL PROTECTED]
> wrote:
> 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.
>
This a most general idea if functors cant be used with <algorithm>
it further does not require ListType to be copiable and assignable
as does <algorithm>.
void modify_list(ListType &list)
{
ListType marked_for_delete;
/*
marked_for_delete contains the items to delete
iterators pointing to these values are valid in that
dereferencing the iterator still points to data and references
are still valid as the data node itself is not moved in memory
only internal ptrs change so incrementing iterators pointing
to elements of marked_for_delete will point to different
values, that is *++before is not equal to *++after in general,
*/
for(ListType::iterator it = list.begin();it != list.end();++it)
{
// determine if we delete this item
if(we_will_delete)
{
marked.for_delete.splice(marked_for_delete.end(),list,it);
}
}
// other operations requiring all the data;
// let marked_for_delete to go out of scope and it will
// destruct the deleted items.
}
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|