-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector_Allfuctions.cpp
137 lines (119 loc) · 2.76 KB
/
Vector_Allfuctions.cpp
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
//***vector initialization***
vector<int> v;
/* to define vector size
vector<int> v(5);
*/
v.push_back(1);
v.push_back(2);
v.push_back(3);
/*
//vector input will continue when it give input as int value
//if not give int value it automattically stop give input
int p;
while(cin>>p){
v.push_back(p);
}
*/
// vector size print
cout << v.size() << endl;
cout << "Capacity : " << v.capacity();
cout << "\nMax_Size : " << v.max_size();
// Vector resize(3 to 5)
v.resize(5);
// Check Vector is empty or not(if empty output:1, otherwise output: 0)
cout << "is it empty?" << v.empty() << endl;
// fill the array with 10 five times
v.assign(5, 10);
// ***Print all vector elements***
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
/*Another approach
//for range
for(int element: v)
cout << element << "\t";
*/
// print fornt and back element
cout << "\nfront() = " << v.front();
cout << "\nback() = " << v.back();
// delete 2nd element
v.erase(v.begin() + 1);
cout << " " << endl;
cout << "After 2nd index delete " << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
// removes last element
v.pop_back();
// inserts 5 at the beginning
v.insert(v.begin(), 5);
// insert an element in position 2
v.insert(v.begin() + 1, 8);
cout << " " << endl;
cout << "After 2nd index insert " << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << " " << endl;
// reverse all vector begin to end
reverse(v.begin(), v.end());
cout << "After Reverse" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
/*
//Another approach to reverse
cout<<"reverse"<<endl;
for (int i = v.size()-1; i >=0; i--)
{
cout << v[i] << " ";
}
*/
cout << " " << endl;
//***sorting vector in accending order***
sort(v.begin(), v.end());
/*sort in decending order
sort(v.begin(), v.end());
reverse(v.begin(), v.end());*/
cout << "After Sort" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
// clear all vector elements
v.clear();
cout << " " << endl;
cout << "is it empty?" << v.empty() << endl;
cout << endl;
//***two vector to perform swap***
vector<int> v1, v2;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v2.push_back(4);
v2.push_back(5);
v2.push_back(6);
cout << "\nBefore Swap \nVector 1: ";
for (int i = 0; i < v1.size(); i++)
cout << v1[i] << " ";
cout << "\nVector 2: ";
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
// Swaps v1 and v2
v1.swap(v2);
cout << "\nAfter Swap \nVector 1: ";
for (int i = 0; i < v1.size(); i++)
cout << v1[i] << " ";
cout << "\nVector 2: ";
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
return 0;
}