fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  7. auto it = v.begin() + 5;
  8. // replace the current element with the back of the vector,
  9. // then shrink the size of the vector by 1.
  10. *it = std::move(v.back());
  11. for (auto n : v) {
  12. std::cout << n << " ";
  13. }
  14. v.pop_back();
  15.  
  16. for (auto n : v) {
  17. std::cout << n << " ";
  18. }
  19. std::cout << "\n";
  20. }
  21.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
1 2 3 4 5 10 7 8 9 10 1 2 3 4 5 10 7 8 9