Demonstrating the STL list erase function : list erase « List « C++






Demonstrating the STL list erase function

  
 

#include <iostream>
#include <cassert>
#include <list>
#include <string>
#include <algorithm>
using namespace std;


int main()
{
  string s("remembering");

  list<char> list1(s.begin(), s.end());

  list<char>::iterator j;

  j = find(list1.begin(), list1.end(), 'i');

  list1.erase(j++);

  list<char>::iterator i;

  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";

  list1.erase(j++);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";

  list1.erase(j++);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";

  list1.erase(list1.begin());
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";


  list1.erase(list1.begin());
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";
  return 0;
}

 /* 
r e m e m b e r n g


r e m e m b e r g


r e m e m b e r


e m e m b e r


m e m b e r



 */       
    
  








Related examples in the same category

1.keep only the top 3 salespeople