fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. vector<int> v; // a vector of integers
  7.  
  8. // Adding elements
  9. v.push_back(10);
  10. v.push_back(20);
  11. v.push_back(30);
  12.  
  13. // Printing elements
  14. cout << "Vector elements: ";
  15. for(int i=0; i<v.size(); i++) {
  16. cout << v[i] << " ";
  17. }
  18. cout << endl;
  19.  
  20. // Accessing an element
  21. cout << "First element: " << v[0] << endl;
  22.  
  23. // Removing last element
  24. v.pop_back();
  25.  
  26. cout << "After pop_back(): ";
  27. for(int x : v) {
  28. cout << x << " ";
  29. }
  30. cout << endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Vector elements: 10 20 30 
First element: 10
After pop_back(): 10 20