logo CodeStepByStep logo

intersection

Language/Type: C++ map collections STL
Related Links:

Write a function named intersection that accepts as parameters two references to maps of strings to integers, and that returns a new map whose contents are the intersection of the two. The intersection of two maps is defined here as the set of key/value pairs that exist in both maps. So if some key K maps to value V in both the first and second map, include it in your result. If K does not exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair from your result. For example, consider the following two maps:

{"Alyssa":100, "Janet":87, "Jeff":88, "Kim":52, "Logan":62, "Stefanie":80, "Sylvia":95, "Whitaker":46}
{"Brian":60, "Jeff":88, "Kim":52, "Lisa":83, "Logan":62, "Stefanie":80, "Sylvia":87, "Whitaker":52}

Calling your function on the preceding maps would return the following new map:

{"Jeff":88, "Kim":52, "Logan":62, "Stefanie":80}
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.