C++ STL | Set 2 (pair) | Practice | GeeksforGeeks

Implement different operations on pairs.

Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of input contains an integer Q denoting the no of queries . Then in the next line are Q space separated queries .
A query can be of five types 
1. a s x y (Adds a pair with string s and values x,y to the vector A at the end )
2. b (returns the size of the vector A)
3. c (prints space separated values of each element of the vector of pairs A)
4. d (sorts the pair's array with respect to the values x,y in ascending order)


Output:
The output for each test case will  be space separated integers denoting the results of each query . 

Constraints:
1<=T<=100
1<=Q<=100

Example:
Input
2
5
a ga 4 5 a ra 1 2 a sh 1 1 d c
4
a geeks 10 12  a code 11 11 b c

Output
sh 1 1 ra 1 2 ga 4 5 
2 geeks 10 12 code 11 11

Explanation :
For the first test case
There are five queries. Queries are performed in this order
1. a ga 4 5   --> vector contents {ga,4,5} 
2. a ra 1 2    --> vector contents {ga,4,5}, {ra,1,2}
3. a sh 1 1    --> vector contents are {ga,4,5}, {ra,1,2}, {sh,1,1}
4. d               --> vector get sorted as {sh,1,1}, {ra,1,2}, {ga,4,5}
5. c               --> vector values printed as 'sh 1 1 ra 1 2 ga 4 5 ' without quotes 

For the sec test case 
There are four queries. Queries are performed in this order
1. a geeks 10 12 --> vector A has {geeks,10,12}  
2. a code 11 11   --> vector A has {geeks,10,12}, {code,11,11}
3. b       --> prints the size of the vector A ie 2
4. c       --> prints the elements of the pair of vectors as 'geeks 10 12 code 11 11' without quotes.


Note:The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
-5
kashibalti556 months ago
jkfheio
0
Kiranjeet Kaur
This comment was deleted.
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
/* 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 x: A)    {        cout << x.first << " " << x.second.first << " " << x.second.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(),             [](pair<string,pair<int,int>> &p1, pair<string,pair<int,int>> &p2)            {                return p1.second < p2.second;            }        );}
0
sagar dhankar2 years ago
sagar dhankar
/* 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(make_pair(str, make_pair(x, y)));     // Your code here}
/* Returns the size of the vector A */int get_size(vector<pair<string,pair<int,int> > > &A){    return A.size();     // Your code here}
/* Prints space separated values of vector A */void print_values(vector<pair<string,pair<int,int> > > &A){    for(int i=0; i<a.size(); ++i)="" cout="" <<="" a[i].first="" <<"="" "<<="" a[i].second.first="" <<"="" "<<="" a[i].second.second="" <<"="" ";="" your="" code="" here="" }="" <b="">bool myCmp(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(), myCmp);     // Your code here}
0
underrated2 years ago
underrated
Sorting through loop:https://ide.geeksforgeeks.o...
0
Yash Parmar2 years ago
Yash Parmar
/* 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){     // Your code here     A.push_back(make_pair(str,make_pair(x,y)));
}/* Returns the size of the vector A */int get_size(vector<pair<string,pair<int,int> > > &A){     // Your code here     return A.size();}/* Prints space separated values of vector A */void print_values(vector<pair<string,pair<int,int> > > &A){     // Your code here     int i;     for(i=0;i<a.size();i++) {="" cout<<a[i].first="" <<"="" "<<a[i].second.first<<"="" "<<a[i].second.second<<"="" ";="" }="" cout<<endl;="" }="" bool="" sortbysec(const="" pair<string,pair<int,int=""> > &A,const pair<string,pair<int,int> >  &B) {     return (A.second < B.second); }/* Sorts the vector A based on value x and y*/void sort_pair(vector<pair<string,pair<int,int> > > &A){     // Your code here     sort(A.begin(),A.end(),sortbysec);}
0
Aman Birla3 years ago
Aman Birla
i didnt get it..how to sort  with respect to both x and y?do we need to take x and y as one single digit and then sort?
0
Muskan Agarwal3 years ago
Muskan Agarwal
AC answer:https://practice.geeksforge...
0
Raghav Goyal3 years ago
Raghav Goyal
Why do my code gives runtime error ?
https://practice.geeksforge...
0
Sammas3 years ago
Sammas
whats wrong with this https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.