-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
159 lines (134 loc) · 5.48 KB
/
Copy pathvector.cpp
File metadata and controls
159 lines (134 loc) · 5.48 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//Welcome to a complete Tutorial of Vectors.
#include<iostream>
//Header file to include Vector
#include<vector>
using namespace std;
//template<class T>//Using Template for generic programming
int main(){
cout<<"Welcome to a complete Tutorial of Vectors:"<<endl<<"First lets learn ways to create vector:"<<endl<<endl<<"First lets create an Empty vector"<<endl;
//First lets learn ways to create vector:
//First lets create an Empty vector
vector<int>a;
cout<<"Size of the empty vector found using .size() function is "<<a.size()<<endl<<endl;
//Next lets create an Empty vector of size 3
cout<<"Next lets create an Empty vector of size 3"<<endl;
vector<float>b(3);
cout<<"Size of the empty vector found using .size() function is "<<b.size()<<endl;
cout<<"Contents of vector using [] operator:"<<b[0]<<b[1]<<b[2]<<endl<<endl;
//Next lets create a vector of size 3 all containing value 6
cout<<"Next lets create a vector of size 3 all containing value 6"<<endl;
vector<int>c(3,6);
cout<<"Size of the vector found using .size() function is "<<c.size()<<endl;
cout<<"Contents of vector using [] operator:"<<c[0]<<c[1]<<c[2]<<endl<<endl;
//lets copy the previous vector in a new vector
cout<<"lets copy the previous vector in a new vector using vector<int>vec(vec1):"<<endl;
vector<int>d(c);
cout<<"Size of the copied vector found using .size() function is "<<d.size()<<endl;
cout<<"Contents of vector using [] operator:"<<d[0]<<d[1]<<d[2]<<endl<<endl;
//lets create first vector of integer type,alloted size of 5
vector<int>v(5);
int ele;
//Another type of float type.Thanks to Template
vector<float>v1;
cout<<"lets create first vector of integer type,alloted size of 5 and Another type of float type.Thanks to Template"<<endl;
cout<<"Enter values into the first vector:"<<endl;
for(int i=0;i<5;i++)
{
cin>>v[i];
}
cout<<"Displaying the values using []:"<<endl;
for(int i=0;i<5;i++)
{
cout<<v[i]<<endl;
}
cout<<"Enter 3 values into the second vector:Note that we will use .push_back() to accept values here"<<endl;
for(int i=0;i<3;i++)
{
cin>>ele;
v1.push_back(ele);
}
cout<<"Displaying the values using .size() to terminate and [] to print:"<<endl;
for(int i=0;i<v1.size();i++)
{
cout<<v1[i]<<endl;
}
//Next lesson is to find current size,maximum size and storage space alloted to a vector using size,max_size and capacity functions
cout<<"Next lesson is to find current size,maximum size and storage space alloted to a vector using size,max_size and capacity functions"<<endl;
cout<<"Size of the previous vector:"<<v1.size()<<endl;
cout<<"Maximum Size of a vector:"<<v1.max_size()<<endl;
cout<<"storage alloted to the vector:"<<v1.capacity()<<endl<<endl;
//Next we will learn how to find value at a particular place using .at
cout<<"Next we will learn how to find value at a particular place using .at() function"<<endl;
cout<<"Lets find 2nd element in previous vector"<<endl;
cout<<"ELement at 2nd position:"<<v1.at(2)<<endl<<endl;
//Next lesson is to find the first and last value of a vector using .front and .back()
cout<<"Next lesson is to find the first and last value of a vector using .front and .back()"<<endl;
cout<<"Value at front="<<v1.front()<<endl;
cout<<"Value at back="<<v1.back()<<endl<<endl;
//Now lets resize the last vector to size 4
cout<<"Now lets resize the last vector to size 4"<<endl;
v1.resize(4);
cout<<"Entering the 4th value as 100"<<endl;
v1.at(3)=100;
cout<<"Update vector:"<<endl;
for(int i=0;i<v1.size();i++)
{
cout<<v1[i]<<endl;
}
cout<<endl;
//Now lets delete last element
cout<<"Now lets delete last element from previous array"<<endl;
v1.pop_back();
cout<<"Updated vector after using pop_back()"<<endl;
for(int i=0;i<v1.size();i++)
{
cout<<v1[i]<<endl;
}
cout<<"Notice how last element is deleted using pop_back()"<<endl<<endl;
//Next lets clear the prev vector and use it again using .clear() function
cout<<"Next lets clear the prev vector and use it again using .clear() function"<<endl;
v1.clear();
//lets check if the prev vector is empty or not using .empty() function
if(!v1.empty())
cout<<"Vector is not empty"<<endl;
else
cout<<"Vector is empty"<<endl;
cout<<"Enter new values:"<<endl;
for(int i=0;i<4;i++)
{
cin>>v1[i];
}
cout<<"Updated vector"<<endl;
for(int i=0;i<4;i++)
{
cout<<v1[i];
}
cout<<endl<<endl;
//Next lesson is to swap 2 vectors using vec1.swap(vec2)
cout<<"Next lesson is to swap 2 vectors using vec1.swap(vec2)"<<endl;
vector<int> foo (3,100); // three ints with a value of 100
cout<<"Vector foo before swap"<<endl;
for(int i=0;i<3;i++)
{
cout<<foo[i];
}
cout<<endl<<"Vector bar before swap"<<endl;
vector<int> bar (5,200); // five ints with a value of 200
for(int i=0;i<5;i++)
{
cout<<bar[i];
}
foo.swap(bar);
cout<<endl<<"Vector foo after swap"<<endl;
for(int i=0;i<5;i++)
{
cout<<foo[i];
}
cout<<endl<<"Vector bar after swap"<<endl;
for(int i=0;i<3;i++)
{
cout<<bar[i];
}
cout<<endl<<endl;
return 0;
}