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

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

by Alex Shulgin <alex.shulgin@[EMAIL PROTECTED] > Apr 25, 2008 at 03:42 AM

On Apr 24, 10:04 am, lallous <lall...@[EMAIL PROTECTED]
> wrote:
> Hello Group,
>
> Please advise. Does this work with all STL implementations?
>
> Thank you,
> Elias
>
> [code]
> #include <iostream>
> #include <list>
>
> typedef std::list<int> int_list_t;
> typedef std::list<int_list_t::iterator> int_list_iterator_list_t;
>
> void print_list(int_list_t &L)
> {
>    for (int_list_t::iterator it=L.begin();it!=L.end();++it)
>    {
>      std::cout << "value = " << *it << std::endl;
>    }
>
> }
>
> void delete_odd(int_list_t &L)
> {
>    int_list_iterator_list_t it_list;
>    int_list_t::iterator it;
>
>    for (it=L.begin();it!=L.end();++it)
>    {
>      if (*it % 2 != 0)
>        it_list.push_back(it);
>    }
>
>    for (int_list_iterator_list_t::const_iterator di=it_list.begin();di!
> =it_list.end();++di)
>    {
>      L.erase(*di);
>    }
>
> }

It should work with any reasonable STL implementation.

But do you really need that list of iterators?  Why not erase matching
list elements right away? :-)

-- 
Cheers,
Alex

      [ 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 Fri Jul 25 21:57:18 CDT 2008.