fork download
  1. /*
  2. The header file that includes every Standard library
  3. */
  4. #include<bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. void print(vector<int> x){
  9. vector <int>::iterator it;
  10. for (it=x.begin(); it!=x.end(); it++) {
  11. cout << *it << " ";
  12. }
  13. cout << '\n';
  14. }
  15.  
  16. int main(){
  17.  
  18.  
  19. //--------------V E C T O R--------------//
  20.  
  21. vector <int> v;
  22.  
  23. if(v.empty()){ //checking if vector is empty
  24. cout << "The vector is empty" << '\n';
  25. }
  26.  
  27. v.push_back(5); //pushing values to vector v
  28. v.push_back(3);
  29. v.push_back(20);
  30. v.push_back(10);
  31. v.push_back(100);
  32. v.push_back(15);
  33.  
  34. //Size of the vector == number of elements in the vector
  35. //v.size()
  36. cout << "Number of elements in the vector : " << v.size() << '\n';
  37.  
  38. // iterating over the vector, declaring the iterator
  39. vector <int>::iterator it;
  40.  
  41. cout << "The vector v contains: ";
  42. for (it=v.begin(); it!=v.end(); it++) {
  43. cout << *it << " ";
  44. }
  45.  
  46. cout << '\n';
  47.  
  48. //getting value at particular position
  49. int pos = 2;
  50. cout << "Element at position " << pos << " is " << v.at(pos) << '\n';
  51.  
  52. //Deleting an element at specific position
  53. v.erase(v.begin() + pos);
  54. //Before deleting : 5 3 20 10 100 15
  55. //After deleting : 5 3 10 100 15
  56.  
  57. cout << "Elements in vector V: ";
  58. print(v); //created a function to print the elements in the vector
  59.  
  60. //Copying the element from one vector to another
  61. //5 3 10 100 15
  62. vector<int>a(v);
  63.  
  64. cout << "Elements in vector a: ";
  65. print(a);
  66.  
  67. //Reversing the vector elements
  68. //15 100 10 3 5
  69. reverse(a.begin(), a.end());
  70.  
  71. cout << "reversing the vector elements: ";
  72. print(a);
  73.  
  74. //Sorting the elements of vector a
  75. //3 5 10 15 100
  76. sort(a.begin(), a.end());
  77.  
  78. cout << "Sorting the elements of vector a : ";
  79. print(a);
  80.  
  81. //Sorting in descending order
  82. //100 15 10 5 3
  83. sort(a.rbegin(), a.rend());
  84.  
  85. cout << "Sorting in descending order : ";
  86. print(a);
  87.  
  88. //Other ways of declaring vector
  89. vector<int> b(5); // vector size initilized to 5
  90. vector<int> c(5,0); // vector of size 5, all 5 elements initlized to 0, ie -> 0,0,0,0,0
  91.  
  92. cout << "The elements of vector C are : ";
  93. print(c);
  94.  
  95. vector<int> d = {1,2,3,3,3,3,3,3,4,4,5,6,7,8,8,8,8,8,8,9}; //initilizing the elements of vector D
  96.  
  97. cout << "The elements of vector D are : ";
  98. print(d);
  99.  
  100. //Counting the occurnace of an element
  101. //Count 3
  102. int cnt = count(d.begin(), d.end(), 3);
  103.  
  104. cout << "The number of occurance of 3 in the vector d -> " << cnt << '\n';
  105.  
  106. //First occurance of an element in vector
  107. int num = 3;
  108. vector<int>::iterator low = lower_bound(d.begin(), d.end(), num);
  109. cout << "First occurance of 3 in vector D -> " << low - d.begin() << '\n';
  110.  
  111. //Last occurance of an element in vector
  112. vector<int>::iterator high = upper_bound(d.begin(), d.end(), num);
  113. cout << "Last occurance of 3 in vector D -> " << high - d.begin() << '\n';
  114.  
  115. //Removing all occurance of a speific element from a vector
  116. //Let us remove 8 from vector D
  117. d.erase(remove(d.begin(), d.end(), 8), d.end());
  118.  
  119. cout << "After removing all occurance of 8 from vector D : ";
  120. print(d);
  121.  
  122. //Removing all multiple occurnace of an element from vector
  123. //or keeping only unique elements in vector
  124. d.erase(unique(d.begin(), d.end()), d.end());
  125.  
  126. cout << "unique elements in vector D : ";
  127. print(d);
  128.  
  129. //Rotate left
  130. //rotating left 1 times
  131. int n = 1;
  132. rotate(d.begin(), d.begin() + n, d.end());
  133.  
  134. cout << "Rotate left : ";
  135. print(d);
  136.  
  137. //Rotate right
  138. //rotating right 3 times
  139. n = 3;
  140. rotate(d.rbegin(), d.rbegin() + n, d.rend());
  141.  
  142. cout << "Rotate right : ";
  143. print(d);
  144.  
  145. vector <int> e = {1,2,3,4,5};
  146. //Sum of all the elements of the vector
  147. int sum = accumulate(e.begin(), e.end(), 0);
  148.  
  149. cout << "The sum of all elemets of vector E is -> " << sum << '\n';
  150.  
  151. //Multiply all elements of vector
  152. int mul = accumulate(e.begin(), e.end(), 1., multiplies<double>());
  153.  
  154. cout << "The product of all elemets of vector E is -> " << mul << '\n';
  155.  
  156. //Search if an element exist in an array
  157. //if exits return 1 else returns 0
  158. int exi = binary_search(e.begin(), e.end(), 5);
  159. cout << "exists -> " << exi << '\n';
  160.  
  161. exi = binary_search(e.begin(), e.end(), 9);
  162. cout << "doesn't exists -> " << exi << '\n';
  163.  
  164. //Insert element at any position of the vector
  165. //at pos 2, we shall insert 100
  166. pos = 2;
  167. num = 100;
  168. e.insert(e.begin()+pos, num);
  169.  
  170. cout << "Vector E after inserting 100 at position 2 -> ";
  171. print(e);
  172.  
  173. //Merging 2 vector elements
  174. vector<int> arr1 = { 1,2,3,4,5 };
  175. vector<int> arr2 = { 6,7,8,9,10 };
  176. vector<int> arr3(10);
  177. merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin());
  178. cout << "Merging 2 vector elements : ";
  179. print(arr3);
  180.  
  181. }
Success #stdin #stdout 0s 5568KB
stdin
Standard input is empty
stdout
The vector is empty
Number of elements in the vector : 6
The vector v contains: 5 3 20 10 100 15 
Element at position 2 is 20
Elements in vector V: 5 3 10 100 15 
Elements in vector a: 5 3 10 100 15 
reversing the vector elements: 15 100 10 3 5 
Sorting the elements of vector a : 3 5 10 15 100 
Sorting in descending order : 100 15 10 5 3 
The elements of vector C are : 0 0 0 0 0 
The elements of vector D are : 1 2 3 3 3 3 3 3 4 4 5 6 7 8 8 8 8 8 8 9 
The number of occurance of 3 in the vector d -> 6
First occurance of 3 in vector D -> 2
Last occurance of 3 in vector D -> 8
After removing all occurance of 8 from vector D : 1 2 3 3 3 3 3 3 4 4 5 6 7 9 
unique elements in vector D : 1 2 3 4 5 6 7 9 
Rotate left : 2 3 4 5 6 7 9 1 
Rotate right : 7 9 1 2 3 4 5 6 
The sum of all elemets of vector E is -> 15
The product of all elemets of vector E is -> 120
exists -> 1
doesn't exists -> 0
Vector E after inserting 100 at position 2 -> 1 2 100 3 4 5 
Merging 2 vector elements : 1 2 3 4 5 6 7 8 9 10