fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. template<typename T> // display an STL container
  9. void dispSTL(const string& str, const T& x)
  10. {
  11. cout << str;
  12. for(auto it = std::begin(x); it!=std::end(x); it++)
  13. cout << *it << " ";
  14. cout << endl;
  15. }
  16.  
  17. // main
  18. int main(void)
  19. {
  20. list<int> lst{1,2,3,4,5};
  21. vector<int> vct{1,2,3,4,5};
  22.  
  23. cout << "std::list" << endl;
  24. dispSTL("Initial: ", lst);
  25. // remove the value 3 from the list
  26. // elements after 3 are left-copied by 1 position
  27. auto pos = remove(lst.begin(), lst.end(), 3);
  28. dispSTL("After remove 3: ", lst);
  29. lst.erase(pos, lst.end());
  30. dispSTL("After list.erase: ", lst);
  31.  
  32. cout << endl << "std::vector" << endl;
  33. dispSTL("Initial: ", vct);
  34. // remove the value 3 from the list
  35. // elements after 3 are left-copied by 1 position
  36. auto pos1 = remove(vct.begin(), vct.end(), 3);
  37. dispSTL("After remove 3: ", vct);
  38. vct.erase(pos1, vct.end());
  39. dispSTL("After vect.erase: ", vct);
  40. }
  41.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
std::list
Initial: 1 2 3 4 5 
After remove 3: 1 2 4 5 5 
After list.erase: 1 2 4 5 

std::vector
Initial: 1 2 3 4 5 
After remove 3: 1 2 4 5 5 
After vect.erase: 1 2 4 5