1 #include<iostream>
2 #include<vector>
3 using namespace std;
4
5 void print( vector<int> &vec )
6 {
7 for ( vector<int>::iterator it = vec.begin(); it!=vec.end(); it++ )
8 cout<<*it<<‘ ‘;
9 cout<<endl;
10 }
11
12 void push( vector<int> &vec, int value )
13 {
14 vec.push_back(value);
15 }
16
17 int main()
18 {
19 vector<int> first;
20 vector<int> second (4,100); // four ints with value 100
21 print(second);
22 vector<int> third ( second.begin()+1, second.end() );
23 print(third);
24 vector<int> fourth (third);
25
26 //construct from arrays
27 int arrays[] = {1,2,3,4,5,6};
28 vector<int> fifth ( arrays, arrays + sizeof(arrays)/sizeof(int) );
29
30 print(fifth);
31 push(fifth, 20);
32 print(fifth);
33 fifth.pop_back();
34 print(fifth);
35 fifth.pop_back();
36 print(fifth);
37
38 cout<<"capacity is "<<fifth.capacity()<<endl;
39 cout<<"size is "<<fifth.size()<<endl;
40 fifth.reserve(20);
41 cout<<"capacity is "<<fifth.capacity()<<endl;
42 cout<<"size is "<<fifth.size()<<endl;
43 print(fifth);
44
45 // erase the first 3 elements:
46 fifth.erase(fifth.begin(), fifth.begin()+3);
47 for (int i=0; i<fifth.size(); i++ )
48 cout<<fifth[i]<<‘ ‘;
49 cout<<endl;
50
51 return 0;
52 }