C++ STL | Set 3 (map) | Practice | GeeksforGeeks

Implement different operations on maps.

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 three types 
1. a x y (adds a value with key x and value y to the map)
2. b x (print value of  x is present in the map else print -1. )
3. c (prints values x and y separated by space i.e., contents of map)

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
4
a 1 2 a 66 3 b 66 c
3
a 1 66 b 5 c

Output
3 1 2 66 3
-1 1 66

Explanation :
For the first test case
There are four queries. Queries are performed in this order
1. a 1 2  -->  map has a key 1 with value 2 
2. a 66 3 --> map has a key 66 with value 3
3. b 66    --> prints the value of key 66 if its present in the map ie 3.
4. c    -->  print the contents of map separated by space ie ( 1 2 66 3 )
For the sec test case 
There are three queries. Queries are performed in this order
1. a 1 66 --> adds a key 1 with a value of 66 in the map
2. b 5      -->  since the key 5 is not present in the map hence -1 is printed.
3. c   --> prints contents of map separated by space ie(1 66)

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.
0
deepaksahu424
This comment was deleted.
0
karntiwari3 months ago
void add_value(map<int,int> &m,int x,int y){   //Your code here   m[x] = y;}
/* Returns the value of the keyx if present else returns -1 */int find_value(map<int,int> &m,int x){ //Your code here if(m.find(x)!=m.end()) return m[x]; return -1;}
/* Prints contents of the map ie keys and values*/void print_contents(map<int,int> &m){  //Your code here'  for(auto x: m)  {      cout<<x.first<<" "<<x.second<<" ";  }}
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
/* Adds a value with key x and value y to the map*/void add_value(map<int,int> &mp,int x,int y){    mp[x] = y;}/* Returns the value of the key x if present else returns -1 */int find_value(map<int,int> &mp, int x){    if(mp.find(x) != mp.end())    {        return mp[x];    }        return -1;}/* Prints contents of the map ie keys and values*/void print_contents(map<int,int> &mp){    for(auto x: mp)    {        cout << x.first << " " << x.second << " ";    }}
0
Amar Prakash1 year ago
Amar Prakash
void add_value(map<int,int> &m,int x,int y){    m[x] = y;    //Your code here}
/* Returns the value of the key x if present else returns -1 */int find_value(map<int,int> &m,int x){    if(m.find(x) != m.end())    {        return m[x];    }    return -1;  //Your code here}
/* Prints contents of the map ie keys and values*/void print_contents(map<int,int> &m){    for( auto x : m)    {        cout<<x.first<<" "<<x.second<<"="" ";="" }="" your="" code="" here="" }="">
0
penny53402 years ago
penny5340
shortest sol ET= 0.01upvote me for more .https://ide.geeksforgeeks.o...
0
Ditikrushna Giri3 years ago
Ditikrushna Giri
Wrong Answer. !!!Wrong AnswerPossibly your code doesn't work correctly for multiple test-cases (TCs).The first test case where your code failed:
Input:34b 27 b 43 b 36 a 21 49 c b 40 b 13 b 40 a 36 22 c c a 32 29 c c b 17 b 29 a 8 22 c a 6 43 b 42 a 21 23 c c b 48 c c a 26 13 b 30 c a 20 12 b 31 a 34 25 a 5 36 b 29
Its Correct output is:-1 -1 -1 21 49 -1 -1 -1 21 49 36 22 21 49 36 22 21 49 32 29 36 22 21 49 32 29 36 22 -1 -1 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 23 32 29 36 22 6 43 8 22 21 23 32 29 36 22 -1 6 43 8 22 21 23 32 29 36 22 6 43 8 22 21 23 32 29 36 22 -1 6 43 8 22 21 23 26 13 32 29 36 22 -1 -1
And Your Code's output is:-1 -1 -1 21 49 -1 -1 -1 21 49 36 22 21 49 36 22 21 49 32 29 36 22 21 49 32 29 36 22 -1 -1 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 32 29 36 22 6 43 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 32 29 36 22 6 43 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 26 13 32 29 36 22 -1 -1Need Help?
What 's  wrong with my output can anybody point it out ?
0
Deepak Yadav4 years ago
Deepak Yadav
see the logic of add_new_valuevoid add_value(map<int,int> &m,int x,int y){    pair<map<int,int>::iterator,bool> ret;    ret=m.insert(pair<int,int>(x,y));    if(ret.second==false)    {        m[x]=y;    }}
0
Dewanshu Singhal4 years ago
Dewanshu Singhal
In Test Case34b 27 b 43 b 36 a 21 49 c b 40 b 13 b 40 a 36 22 c c a 32 29 c c b 17 b 29 a 8 22 c a 6 43 b 42 a 21 23 c c b 48 c c a 26 13 b 30 c a 20 12 b 31 a 34 25 a 5 36 b 29
Its Correct output is:-1 -1 -1 21 49 -1 -1 -1 21 49 36 22 21 49 36 22 21 49 32 29 36 22 21 49 32 29 36 22 -1 -1 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 23 32 29 36 22 6 43 8 22 21 23 32 29 36 22 -1 6 43 8 22 21 23 32 29 36 22 6 43 8 22 21 23 32 29 36 22 -1 6 43 8 22 21 23 26 13 32 29 36 22 -1 -1
And My Code's Output is:-1 -1 -1 21 49 -1 -1 -1 21 49 36 22 21 49 36 22 21 49 32 29 36 22 21 49 32 29 36 22 -1 -1 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 32 29 36 22 6 43 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 32 29 36 22 6 43 8 22 21 49 32 29 36 22 -1 6 43 8 22 21 49 26 13 32 29 36 22 -1 -1
First in test case it is inserting  "a 21 49" and later again it is inserting "a 21 23". But in map we can not insert two values with same index. So a 21 23 will not get inserted in map. So how in Correct Output it is displaying 21 23?In my output it is displaying 21 49, which is think is correct
0
Naman Soni5 years ago
Naman Soni
http://ide.geeksforgeeks.or...
What is wrong with my code?Giving the o/p as expected but wrong answer.
0
Quandray5 years ago
Quandray
The example input is missing a line. It should say24a 1 2 a 66 3 b 66 c3a 1 66 b 5 c
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.