Question: map can be considered as a set of (key, value) pairs where no key appears twice. We want to define a function 'checkProperty' which takes

map can be considered as a set of (key, value) pairs where no key appears twice.

We want to define a function 'checkProperty' which takes a map object and checks if the map satisfies the following property:

The set of all keys of a map is equal to the set of all values of a map

For Example: If we consider an object 'mp' of type map, with mp[a] = b, mp[b] = x and mp[c] = x then the set of all keys is {a, b, c} and the set of values is {b,x} which are not equal

The function 'checkProperty' should returns true if the property is satisfied, else it should return false.

Choose the options which correctly implement the above said property.(There can be multiple correct options.)

 //1 solution  bool checkProperty(map<string,string> &mp) { map<string,bool> temp; for(map<string,string>::iterator it = mp.begin(); it != mp.end(); ++it) { temp[it->first] = true; temp[it->second] = true; } return temp.size() == mp.size(); } //2 solution  bool checkProperty(map<string,string> &mp) { map<string,bool> temp; for(map<string,string>::iterator it = mp.begin(); it != mp.end(); it++) { temp[it->second] = true; } return temp.size() == mp.size(); } //3 solution  bool checkProperty(map<string,string> &mp) { map<string,bool> tempN; map<string,bool> tempV; for(map<string,string>::iterator it = mp.begin(); it != mp.end(); it++) { tempV[it->second] = true; tempN[it->first] = true; tempN[it->second] = true; } return (tempN.size() == mp.size()) && (tempV.size() == mp.size()); } //4 solution  bool checkProperty(map<string,string> &mp) { map<string,bool> tempV; map<string,bool> tempN; for(map<string,string>::iterator it = mp.begin(); it != mp.end(); it++) { if(tempV.count(it->second)) return false; tempV[it->second] = true; tempN[it->first] = true; tempN[it->second] = true; } return (tempN.size() == mp.size()); } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!