-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++ STL Set 1 (vector).cpp
105 lines (91 loc) · 1.93 KB
/
C++ STL Set 1 (vector).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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
void add_to_vector(vector<int> &A, int x);
void sort_vector_asc(vector<int> &A);
void reverse_vector(vector<int> &A);
int size_of_vector(vector<int> &A);
void sort_vector_desc(vector<int> &A);
void print_vector(vector<int> &A);
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
vector<int> A;
int q;
cin >> q;
while (q--)
{
char c;
cin >> c;
if (c == 'a')
{
int x;
cin >> x;
add_to_vector(A, x);
}
if (c == 'b')
{
sort_vector_asc(A);
}
if (c == 'c')
{
reverse_vector(A);
}
if (c == 'd')
{
cout << size_of_vector(A) << " ";
}
if (c == 'e')
{
print_vector(A);
}
if (c == 'f')
{
sort_vector_desc(A);
}
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
/*You are required to complete below methods*/
/*inserts an element x at
the back of the vector A */
void add_to_vector(vector<int> &A, int x)
{
A.push_back(x);
}
/*sort the vector A in ascending order*/
void sort_vector_asc(vector<int> &A)
{
sort(A.begin(), A.end());
}
/*reverses the vector A*/
void reverse_vector(vector<int> &A)
{
reverse(A.begin(), A.end());
}
/*returns the size of the vector A */
int size_of_vector(vector<int> &A)
{
return A.size();
}
/*sorts the vector A in descending order*/
void sort_vector_desc(vector<int> &A)
{
sort(A.begin(), A.end(), greater<int>());
}
/*prints space separated
elements of vector A*/
void print_vector(vector<int> &A)
{
for (int i = 0; i < A.size(); i++)
{
cout << A[i] << " ";
}
}