fork download
  1. //
  2. // main.cpp
  3. // Vector (STL)
  4. //
  5. // Created by Himanshu on 23/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. void printIntVector (vector<int> l) {
  14. vector<int>::iterator it;
  15. for (it = l.begin(); it != l.end(); it++) {
  16. cout<<(*it)<<" ";
  17. }
  18. cout<<endl;
  19. }
  20.  
  21. void printFloatVector (vector<float> l) {
  22. vector<float>::iterator it;
  23. for (it = l.begin(); it != l.end(); it++) {
  24. cout<<(*it)<<" ";
  25. }
  26. cout<<endl;
  27. }
  28.  
  29. int main () {
  30.  
  31. // Declaration
  32. vector<float> first, second;
  33.  
  34. // Initialisation
  35. vector<int> vectorFirst({10, 20, 15});
  36.  
  37. // vectorSecond is intialised with 3 ints
  38. // having value as 45
  39. vector<int> vectorSecond(3, 45);
  40.  
  41. cout<<"vectorFirst elements:"<<endl;
  42. printIntVector(vectorFirst);
  43.  
  44. cout<<"vectorSecond elements:"<<endl;
  45. printIntVector(vectorSecond);
  46.  
  47. int arr[3] = {100, 101, 102};
  48. vector<int> vectorThird;
  49.  
  50. vectorThird.insert(vectorThird.begin(), arr, arr+3);
  51.  
  52. cout<<"vectorThird elements after initialisation using arr:"<<endl;
  53. printIntVector(vectorThird);
  54. cout<<endl;
  55.  
  56.  
  57. cout<<"vectorFirst first element: "<<vectorFirst.front()<<endl;
  58. cout<<"vectorFirst last element: "<<vectorFirst.back()<<endl;
  59.  
  60.  
  61. vector<int>::iterator it;
  62.  
  63. //Iterator to vector's first element
  64. it = vectorFirst.begin();
  65. //insert() returns pointer to the (new) first element
  66. it = vectorFirst.insert (it, 2);
  67.  
  68. cout<<"vectorFirst elements after adding 2 at position (it):"<<endl;
  69. printIntVector(vectorFirst);
  70.  
  71. //Iterator to 3rd element after vectorFirst.begin()
  72. vectorFirst.insert (it+3, 2, 300);
  73.  
  74. cout<<"vectorFirst elements after adding 2 elements (value: 300) at position (it+3):"<<endl;
  75. printIntVector(vectorFirst);
  76.  
  77.  
  78.  
  79. vectorFirst.swap(vectorSecond);
  80.  
  81. cout<<"vectorFirst elements after swap:"<<endl;
  82. printIntVector(vectorFirst);
  83. cout<<"vectorSecond elements after swap:"<<endl;
  84. printIntVector(vectorSecond);
  85.  
  86. vectorFirst.clear();
  87. cout<<"vectorFirst elements after clear:"<<endl;
  88. printIntVector(vectorFirst);
  89.  
  90. if (vectorFirst.empty()) {
  91. cout<<"vectorFirst is empty"<<endl;
  92. }
  93.  
  94. // push_back – adds a new element
  95. // at the end of the vector
  96. first.push_back (1.0);
  97. first.push_back (2.0);
  98. first.push_back (3.0);
  99. first.push_back (4.0);
  100. first.push_back (5.0);
  101. first.push_back (6.0);
  102.  
  103.  
  104. cout<<"first vector elements after push_back():"<<endl;
  105. printFloatVector(first);
  106.  
  107.  
  108. cout<<"first vector elements after pop_back():"<<endl;
  109. first.pop_back();
  110. printFloatVector(first);
  111.  
  112.  
  113. // erase – removes either a single element (position)
  114. // or a range of elements ([first, last))
  115. first.erase(first.begin() + 1, first.end() - 2);
  116.  
  117. cout<<"first vector elements after removing the elements from 2 to 4 [2..4)"<<endl;
  118. printFloatVector(first);
  119.  
  120. return 0;
  121. }
Success #stdin #stdout 0.01s 5548KB
stdin
Standard input is empty
stdout
vectorFirst elements:
10 20 15 
vectorSecond elements:
45 45 45 
vectorThird elements after initialisation using arr:
100 101 102 

vectorFirst first element: 10
vectorFirst last element: 15
vectorFirst elements after adding 2 at position (it):
2 10 20 15 
vectorFirst elements after adding 2 elements (value: 300) at position (it+3):
2 10 20 300 300 15 
vectorFirst elements after swap:
45 45 45 
vectorSecond elements after swap:
2 10 20 300 300 15 
vectorFirst elements after clear:

vectorFirst is empty
first vector elements after push_back():
1 2 3 4 5 6 
first vector elements after pop_back():
1 2 3 4 5 
first vector elements after removing the elements from 2 to 4 [2..4)
1 4 5