fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // Creating empty vector containing integers
  8. vector<int> array;
  9. // Creating vector containing 10 elements of type integer with default value for that type - 0
  10. vector<int> array2(10);
  11.  
  12. cout << "Element 0 of array2 is: " << array2[0] << endl;
  13.  
  14. // Creating vector containing 10 elements of type integer with value 23
  15. vector<int> array3(10, 23);
  16.  
  17. cout << "Element 0 of array3 is: " << array3[0] << endl;
  18.  
  19. // We can also achieve similar behaviour by setting value for existing variable
  20. array = vector<int>(10, 23);
  21. cout << "Element 0 of array is: " << array[0] << endl;
  22.  
  23. // Checking size of the array
  24. cout << "Size of the array is: " << array.size() << endl;
  25.  
  26. // Adding new element to the end of the array
  27. array.push_back(55);
  28. cout << "Last element of array is: " << array[array.size() - 1] << endl;
  29. cout << "Size of the array is: " << array.size() << endl;
  30.  
  31. // Adding new element at the position 5 in the array
  32. array.insert(array.begin() + 5, 60);
  33. cout << "Element 5 of array is: " << array[5] << endl;
  34. cout << "Size of the array is: " << array.size() << endl;
  35.  
  36. // Deleting last element in the array
  37. array.pop_back();
  38. cout << "Size of the array is: " << array.size() << endl;
  39.  
  40. // Deleting element at the position 5
  41. array.erase(array.begin() + 5);
  42. cout << "Element 5 of array is: " << array[5] << endl;
  43. cout << "Size of the array is: " << array.size() << endl;
  44.  
  45. // Deleting elements in the array from index 5 to 7, not including 7
  46. array.erase(array.begin() + 5, array.begin() + 7);
  47. cout << "Size of the array is: " << array.size() << endl;
  48.  
  49. // Resizing array to size 20, filling new elements with default value
  50. array.resize(20);
  51. cout << "Element 19 of the array is: " << array[19] << endl;
  52. cout << "Size of the array is: " << array.size() << endl;
  53.  
  54. // Resizing array to size 30, filling new elements with the value 41
  55. array.resize(30, 41);
  56. cout << "Element 29 of the array is: " << array[29] << endl;
  57. cout << "Size of the array is: " << array.size() << endl;
  58.  
  59. // Checking if the array is empty
  60. if(array.empty()) {
  61. cout << "Array is empty" << endl;
  62. } else {
  63. cout << "Array is not empty" << endl;
  64. }
  65.  
  66. cout << "Content of the array: ";
  67. // Printing content of the array
  68. for(auto el: array) {
  69. cout << el << " ";
  70. }
  71.  
  72. cout << endl;
  73.  
  74. // Clearing array removing all of its content
  75. array.clear();
  76. cout << "Size of the array is: " << array.size() << endl;
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 5644KB
stdin
Standard input is empty
stdout
Element 0 of array2 is: 0
Element 0 of array3 is: 23
Element 0 of array is: 23
Size of the array is: 10
Last element of array is: 55
Size of the array is: 11
Element 5 of array is: 60
Size of the array is: 12
Size of the array is: 11
Element 5 of array is: 23
Size of the array is: 10
Size of the array is: 8
Element 19 of the array is: 0
Size of the array is: 20
Element 29 of the array is: 41
Size of the array is: 30
Array is not empty
Content of the array: 23 23 23 23 23 23 23 23 0 0 0 0 0 0 0 0 0 0 0 0 41 41 41 41 41 41 41 41 41 41 
Size of the array is: 0