1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
const int NUMELS = 4;
int n[] ={136, 122, 109, 146};
int i;
// create a vector of strings using the n[] array
vector<int> partnums(n, n + NUMELS);
cout << "\nThe vector initially has a size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
// modify the element at position 4 (i.e. index = 3) in the vector
partnums[3] = 144;
cout << "\n\nAfter replacing the fourth element, the vector has a size of "
<< int(partnums.size()) << ",\n and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
// insert an element into the vector at position 2 (i.e. index = 1)
partnums.insert(partnums.begin()+1, 142);
cout << "\n\nAfter inserting an element into the second position,"
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< " and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
// add an element to the end of the vector
partnums.push_back(157);
cout << "\n\nAfter adding an element to the end of the list,"
<< "\n the vector has a size of " << int(partnums.size()) << ","
<< " and contains the elements:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
// sort the vector
sort(partnums.begin(), partnums.end());
cout << "\n\nAfter sorting, the vector's elements are:\n";
for (i = 0; i < int(partnums.size()); i++)
cout << partnums[i] << " ";
cout << endl;
return 0;
}
|