-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorstl.cpp
More file actions
59 lines (51 loc) · 1.3 KB
/
Copy pathVectorstl.cpp
File metadata and controls
59 lines (51 loc) · 1.3 KB
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<vector>
using namespace std;
int main()
{
vector<int>v;
cout<<v.capacity()<<endl; //0
v.push_back(1);
cout<<v.capacity()<<endl; //1
v.push_back(2);
cout<<v.capacity()<<endl; //2
v.push_back(3);
cout<<v.capacity()<<endl; //4
//capacity doubles after every push_back;
v.push_back(4);
cout<<v.capacity()<<endl; //space created for fourth element so it is 4
v.push_back(5);
cout<<v.capacity()<<endl; //8
//accessing vector elements
cout<<"before pop back"<<endl;
for(int i:v)
{
cout<<i<<" ";
}
cout<<endl;
v.pop_back(); //deleting from last
cout<<"after pop back"<<endl;
for(int i:v)
{
cout<<i<<" ";
}
cout<<endl;
cout<<v.capacity()<<endl; //as already 8 is created
cout<<"size is:"<<v.size()<<endl; //no.of elements
cout<<"at index 2:"<<v.at(2)<<endl; //index starts as array i.e. 0 so at 2: 3
// cout<<v.clear();
// cout<<v.capacity()<<endl;
vector<int>v1(5,1); //initialising all 5 elements of v1 as 1
cout<<"printing v"<<endl;
for(int i:v1)
{
cout<<i<<" ";
}
cout<<endl;
vector<int>last(v1); //copies elements of v1 in vector last
cout<<"printing last"<<endl;
for(int i:last)
{
cout<<i<<" ";
}
}