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
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
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
Get step-by-step solutions from verified subject matter experts
