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






#include <iostream>
#include <cassert>
#include <list>
#include <string>
#include <algorithm>  // for find
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 << " ";
  cout << "\n\n\n";
  // j now points to the 'n':
  list1.erase(j++);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";
  cout << "\n\n\n";

  // j now points to the 'g':
  list1.erase(j++);
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";
  cout << "\n\n\n";
  list1.erase(list1.begin());
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";
  cout << "\n\n\n";

  list1.erase(list1.begin());
  for (i = list1.begin(); i != list1.end(); ++i)
    cout << *i << " ";
  cout << "\n\n\n";
  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








17.6.list erase
17.6.1.erase from begin()
17.6.2.Erasing Elements in a list
17.6.3.listAnother.erase(listAnother.begin(), listAnother.end());
17.6.4.Demonstrating the STL list erase function