fork download
  1. /*
  2. created by Rian Adam Rajagede
  3. 31 July 2020
  4. */
  5.  
  6. #include <iostream>
  7. #include <vector>
  8. using namespace std;
  9.  
  10. int main() {
  11. cout << "=====================Section A======================" << endl;
  12. cout << "====================================================" << endl;
  13.  
  14. // vector of int
  15. vector<int> v;
  16. cout << "Initial vector size:" << v.size() << endl; // vector is empty
  17.  
  18. v.push_back(3);
  19. v.push_back(1);
  20. v.push_back(7);
  21. cout << "New vector size:" << v.size() << endl; // vector size is 3 [3, 1, 7]
  22. cout << "v[0]:" << v[0] << endl;
  23. cout << "v[1]:" << v[1] << endl;
  24. cout << "v[2]:" << v[2] << endl;
  25. cout << "front:" << v.front() << endl;
  26. cout << "back:" << v.back() << endl;
  27.  
  28. cout << "=====================Section B======================" << endl;
  29. cout << "====================================================" << endl;
  30.  
  31. vector<int> v2;
  32. cout << "Initial vector size:" << v2.size() << endl; // vector is empty
  33. v2.assign(100,2);
  34. cout << "New vector size:" << v2.size() << endl; // vector size is 100
  35. cout << "v[0]:" << v2[0] << endl;
  36. cout << "v[1]:" << v2[1] << endl;
  37. cout << "v[2]:" << v2[2] << endl;
  38. cout << "front:" << v2.front() << endl;
  39. cout << "back:" << v2.back() << endl;
  40.  
  41. cout << "=====================Section C======================" << endl;
  42. cout << "====================================================" << endl;
  43.  
  44. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  45.  
  46. v.insert(v.begin(), 2); // insert in the first position
  47. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  48.  
  49. v.insert(v.begin()+1, 8); // insert in the first position+1
  50. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  51.  
  52. v.insert(v.end(), 0); // insert in the last position
  53. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  54.  
  55. v.insert(v.end()-1, 6); // insert in the last position-1
  56. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  57.  
  58. cout << "=====================Section D======================" << endl;
  59. cout << "====================================================" << endl;
  60.  
  61. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  62.  
  63. v.erase(v.begin()+2); // delete 1 elements in position 2
  64. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  65.  
  66. v.erase(v.begin()+1, v.begin()+4); // delete 3 elementa, position 1, 2, and 3
  67. for(int i=0;i<v.size();i++) cout << v[i] << " "; cout << endl; // print elements of v
  68.  
  69. cout << "=====================Section E======================" << endl;
  70. cout << "====================================================" << endl;
  71.  
  72. v.clear();
  73. cout << "Size after cleared:" << v.size() << endl;
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0s 4468KB
stdin
Standard input is empty
stdout
=====================Section A======================
====================================================
Initial vector size:0
New vector size:3
v[0]:3
v[1]:1
v[2]:7
front:3
back:7
=====================Section B======================
====================================================
Initial vector size:0
New vector size:100
v[0]:2
v[1]:2
v[2]:2
front:2
back:2
=====================Section C======================
====================================================
3 1 7 
2 3 1 7 
2 8 3 1 7 
2 8 3 1 7 0 
2 8 3 1 7 6 0 
=====================Section D======================
====================================================
2 8 3 1 7 6 0 
2 8 1 7 6 0 
2 6 0 
=====================Section E======================
====================================================
Size after cleared:0