fork download
  1. //=========================================================
  2. // File: HW_10b
  3. // Programmer: Elaine Torrez
  4. // Class: CMPR 121
  5. //=========================================================
  6. // Description:
  7. // This program demonstrates vector functions.
  8. // It pushes values into a vector, inserts
  9. // values, removes values, and displays the
  10. // size of the vector.
  11. //=========================================================
  12.  
  13. #include <iostream>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. int main()
  19. {
  20. vector<int> values;
  21.  
  22. int index;
  23.  
  24. values.push_back(1);
  25. values.push_back(2);
  26. values.push_back(4);
  27. values.push_back(9);
  28. values.push_back(5);
  29.  
  30. cout << "Vector: ";
  31.  
  32. for (index = 0; index < values.size(); index++)
  33. {
  34. cout << values[index] << " ";
  35. }
  36.  
  37. cout << endl << endl;
  38.  
  39. values.insert(values.begin(), 3);
  40.  
  41. cout << "Vector: ";
  42.  
  43. for (index = 0; index < values.size(); index++)
  44. {
  45. cout << values[index] << " ";
  46. }
  47.  
  48. cout << endl << endl;
  49.  
  50. values.erase(values.begin());
  51.  
  52. cout << "Vector: ";
  53.  
  54. for (index = 0; index < values.size(); index++)
  55. {
  56. cout << values[index] << " ";
  57. }
  58.  
  59. cout << endl << endl;
  60.  
  61. values.pop_back();
  62.  
  63. cout << "Vector: ";
  64.  
  65. for (index = 0; index < values.size(); index++)
  66. {
  67. cout << values[index] << " ";
  68. }
  69.  
  70. cout << endl << endl;
  71.  
  72. cout << "There are "
  73. << values.size()
  74. << " values."
  75. << endl;
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Vector: 1 2 4 9 5 

Vector: 3 1 2 4 9 5 

Vector: 1 2 4 9 5 

Vector: 1 2 4 9 

There are 4 values.