-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
43 lines (32 loc) · 1.12 KB
/
vector.cpp
File metadata and controls
43 lines (32 loc) · 1.12 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
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector < int > v;
for (int i = 0; i < 10; i++) {
v.push_back(i); //inserting elements in the vector
}
cout << "the elements in the vector: ";
for (auto it = v.begin(); it != v.end(); it++)
cout << * it << " ";
cout << "\nThe front element of the vector: " << v.front();
cout << "\nThe last element of the vector: " << v.back();
cout << "\nThe size of the vector: " << v.size();
cout << "\nDeleting element from the end: " << v[v.size() - 1];
v.pop_back();
cout << "\nPrinting the vector after removing the last element:" << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << "\nInserting 5 at the beginning:" << endl;
v.insert(v.begin(), 5);
cout << "The first element is: " << v[0] << endl;
cout << "Erasing the first element" << endl;
v.erase(v.begin());
cout << "Now the first element is: " << v[0] << endl;
if (v.empty())
cout << "\nvector is empty";
else
cout << "\nvector is not empty" << endl;
v.clear();
cout << "Size of the vector after clearing the vector: " << v.size();
}