Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions