C++ STL | Set 7 (unordered map) | Practice | GeeksforGeeks

Implement different operations on unordered 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 four types 
1. a x y (adds an entry with key x and value y to the unordered map)
2. b x (print value of  x if present in the unordered map else print -1. )
3. c (prints the size of the unordered map)
4. d x (removes an entry with key x from the unordered 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(To be used only for expected output):
Input
2
5
a 1 2 a 66 3 b 66 d 1 c
3
a 1 66 b 5 c

Output
3 1
-1 1
Explanation :
For the first test case
There are five 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. d 1        --->  erases an entry from map with key 1
5. c           ---> prints the size of the map ie 1

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 the size of the map ie 1



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
abhijeetkumarsingh2973 weeks ago
Why we can't use insert() and make_pair() functions in add_value()?
+1
badgujarsachin837 months ago
+1
badgujarsachin837 months ago
void add_value(unordered_map<int,int> &m,int x,int y)
{
	//Your code here
	m[x]=y;
}

/*Returns the value with key x from the map */
int find_value(unordered_map<int,int> &m,int x)
{
      //Your code here
      if(m.find(x)!=m.end()){
          return m[x];
      }
      return -1;
}

/*Returns the size of the map */
int getSize(unordered_map<int,int> &m)
{
	//Your code here

        	return m.size();
    
}

/*Removes the entry with key x from the map */
void removeKey(unordered_map<int,int> &m,int x)
{
	//Your code here
	    if(m.find(x)!=m.end()){
	m.erase(x);
	    }
}
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
/*Inserts an entry with key x and value y in map */void add_value(unordered_map<int,int> &m,int x,int y){    m[x] = y;}/*Returns the value with key x from the map */int find_value(unordered_map<int,int> &m,int x){    if(m.find(x) != m.end())    {        return m[x];    }        return -1;}/*Returns the size of the map */int getSize(unordered_map<int,int> &m){    return m.size();}/*Removes the entry with key x from the map */void removeKey(unordered_map<int,int> &m,int x){    if(m.find(x) != m.end())    {        m.erase(x);    }}
0
samir raj2 years ago
samir raj
#simple codevoid add_value(unordered_map<int,int> &m,int x,int y){  m[x]=y;}
int find_value(unordered_map<int,int> &m,int x){//Your codeif(m.find(x)!=m.end())return m[x];return -1;}
int getSize(unordered_map<int,int> &m){    return m.size();}
/*Removes the entry with key x from the map */void removeKey(unordered_map<int,int> &m,int x){      m.erase(x);  //Your code here}
0
Rishabh Sharma2 years ago
Rishabh Sharma
void add_value(unordered_map<int,int> &m,int x,int y){//Your code herem[x]=y;}
int find_value(unordered_map<int,int> &m,int x){      //Your code  if(m.find(x)!=m.end())           return m[x];      return -1;}
int getSize(unordered_map<int,int> &m){int s=0;//Your code herefor(auto i=m.begin();i!=m.end();i++){s++;}
    return s;}
/*Removes the entry with key x from the map */void removeKey(unordered_map<int,int> &m,int x){//Your code herem.erase(x);}
0
penny53402 years ago
penny5340
someone share the submitted code .
0
mmmmm4 years ago
mmmmm
mm
0
krishna6 years ago
krishna
i am having problem in this testcase
34c a 55 91 d 47 a 1 30 c a 50 98 c d 34 d 98 b 93 c b 53 a 89 76 b 30 a 15 61 b 61 a 28 29 c a 3 57 c b 100 d 40 c a 1 4 b 48 c d 52 d 35 d 93 d 28 a 50 30 a 30 86 d 36 b 1
Its Correct output is:0 2 3 -1 3 -1 -1 -1 6 7 -1 7 -1 7 4
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.