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

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

by lallous <lallous@[EMAIL PROTECTED] > Apr 26, 2008 at 04:44 AM

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! ]
 




 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 20:00:44 CDT 2008.