-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathC++ STL Set 2 (pair).cpp
80 lines (74 loc) · 1.87 KB
/
C++ STL Set 2 (pair).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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
void add_pair(vector<pair<string, pair<int, int>>> &A, string str, int x, int y);
int get_size(vector<pair<string, pair<int, int>>> &A);
void print_values(vector<pair<string, pair<int, int>>> &A);
void sort_pair(vector<pair<string, pair<int, int>>> &A);
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
vector<pair<string, pair<int, int>>> A;
int q;
cin >> q;
while (q--)
{
char c;
cin >> c;
if (c == 'a')
{
string name;
cin >> name;
int x, y;
cin >> x >> y;
add_pair(A, name, x, y);
}
if (c == 'b')
{
cout << get_size(A) << " ";
}
if (c == 'c')
{
print_values(A);
}
if (c == 'd')
{
sort_pair(A);
}
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
/* Inserts a pair <string, pair<x, y> > > into the vector A */
void add_pair(vector<pair<string, pair<int, int>>> &A, string str, int x, int y)
{
A.push_back({str, {x, y}});
}
/* Returns the size of the vector A */
int get_size(vector<pair<string, pair<int, int>>> &A)
{
return A.size();
}
/* Prints space separated values of vector A */
void print_values(vector<pair<string, pair<int, int>>> &A)
{
for (auto it : A)
{
cout << it.first << " " << it.second.first << " " << it.second.second << " ";
}
}
bool cmp(pair<string, pair<int, int>> &p1, pair<string, pair<int, int>> &p2)
{
return p1.second < p2.second;
}
/* Sorts the vector A based on value x and y*/
void sort_pair(vector<pair<string, pair<int, int>>> &A)
{
sort(A.begin(), A.end(), cmp);
}