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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started