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++ > Deleting items ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 45394 of 48417
Post > Topic >>

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

by lallous <lallous@[EMAIL PROTECTED] > Apr 23, 2008 at 12:21 PM

#include <conio>
#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);
  }
}

void populate_list(int_list_t &L, int start, int end)
{
  L.clear();
  for (int i=start;i<=end;i++)
    L.push_back(i);
}

int main()
{
  int_list_t L;

  populate_list(L, 1, 10);
  print_list(L);

  std::cout << "---------------------" << std::endl;

  delete_odd(L);
  print_list(L);

  return 0;
}

Please advise. Does this work with all STL implementations?

Thank you,
Elias
 




 7 Posts in Topic:
Deleting items from an std::list , is this code correct?
lallous <lallous@[EMAI  2008-04-23 12:21:57 
Re: Deleting items from an std::list , is this code correct?
"Victor Bazarov"  2008-04-23 15:34:53 
Re: Deleting items from an std::list , is this code correct?
Jay <JSprenkle@[EMAIL   2008-04-23 12:55:57 
Re: Deleting items from an std::list , is this code correct?
Juha Nieminen <nospam@  2008-04-23 21:54:10 
Re: Deleting items from an std::list , is this code correct?
Ramon F Herrera <ramon  2008-04-23 15:18:30 
Re: Deleting items from an std::list , is this code correct?
lallous <lallous@[EMAI  2008-04-23 22:26:44 
Re: Deleting items from an std::list , is this code correct?
lallous <lallous@[EMAI  2008-04-23 22:30:59 

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 Nov 21 13:17:22 CST 2008.