listrmv.cpp 994 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // listrmv.cpp -- applying the STL to a string
  2. #include <iostream>
  3. #include <list>
  4. #include <algorithm>
  5. void Show(int);
  6. const int LIM = 10;
  7. int main()
  8. {
  9. using namespace std;
  10. int ar[LIM] = {4, 5, 4, 2, 2, 3, 4, 8, 1, 4};
  11. list<int> la(ar, ar + LIM);
  12. list<int> lb(la);
  13. cout << "Original list contents:\n\t";
  14. for_each(la.begin(), la.end(), Show);
  15. cout << endl;
  16. la.remove(4);
  17. cout << "After using the remove() method:\n";
  18. cout << "la:\t";
  19. for_each(la.begin(), la.end(), Show);
  20. cout << endl;
  21. list<int>::iterator last;
  22. last = remove(lb.begin(), lb.end(), 4);
  23. cout << "After using the remove() function:\n";
  24. cout << "lb:\t";
  25. for_each(lb.begin(), lb.end(), Show);
  26. cout << endl;
  27. lb.erase(last, lb.end());
  28. cout << "After using the erase() method:\n";
  29. cout << "lb:\t";
  30. for_each(lb.begin(), lb.end(), Show);
  31. cout << endl;
  32. // cin.get();
  33. return 0;
  34. }
  35. void Show(int v)
  36. {
  37. std::cout << v << ' ';
  38. }