-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector
71 lines (57 loc) · 2.19 KB
/
Vector
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
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>v; //declaring vector v
vector<int> a(5,1); //another method of initializing the vector(here vector a is of size 5 nd all elements are initialized by 1)
vector<int> xyz(a); //to copy all elements of vector a
cout<<"print xyz"<<endl;
for(int i:xyz)
{
cout<<i<<" ";
}cout<<endl;
cout<<"Capacity-->"<<v.capacity()<<endl; //capicity finds the memory allowcated to vector v
v.push_back(1); //to enter the elements to a vector from back
cout<<"Capacity-->"<<v.capacity()<<endl;
v.push_back(2);
cout<<"Capacity-->"<<v.capacity()<<endl;
v.push_back(3);
cout<<"Capacity-->"<<v.capacity()<<endl; ------OUTPUT----- Capacity-->4
cout<<"Size-->"<<v.size()<<endl; ------OUTPUT----- Size-->3
cout<<"Element at 2nd Index-->"<<v.at(2)<<endl; //print the element at index 2
cout<<"Empty or not-->"<<v.empty()<<endl; //tells whether a vector is empty or not(0-false;1-true)
cout<<"First element-->"<<v.front()<<endl; //prints the first element of a vector
cout<<"Last element-->"<<v.back()<<endl; //prints the last element of a vector
cout<<"before pop"<<endl;
for(int i:v)
{
cout<<i<<" ";
}cout<<endl;
v.pop_back(); //to remove one element from the last
cout<<"after pop"<<endl;
for(int i:v)
{
cout<<i<<" ";
}
cout<<"Before clear size "<<v.size()<<endl;
v.clear(); //to remove all elements from vector v(but it's size becames 0 not capacity: Capacity remains as it is)
cout<<"After clear size "<<v.size()<<endl;
------------------------OUTPUT----------------------
print xyz
1 1 1 1 1
Capacity-->0
Capacity-->1
Capacity-->2
Capacity-->4
Size-->3
Element at 2nd Index-->3
Empty or not-->0
First element-->1
Last element-->3
before pop
1 2 3
after pop
1 2
Before clear size 2
After clear size 0