Is there a (builtin) way to compare the contents of two stl maps? (Assuming of course that the maps have the same data types).

I thought of writing this myself, but iterating through each map and seeing if the contents are found in the other map (and then doing the reverse), but was wondering if this exists already...

Recommended Answers

All 7 Replies

>Is there a (builtin) way to compare the contents of two stl maps?
Have you tried the relational operators? Or did you just come running here before attempting anything so that you could waste time waiting for an answer?

Go to google, type in "compare C++ maps", and press the I'm Feeling Lucky button.

All of the operators return bool, I was looking for something that details the differences (ie 'X' appears in map 1 and not in map 2, 'Y' appears in map 2 and not map1, etc.). I wasn't looking for whether or not the whole thing is equal or not...

Then be more specific. Which of the following do you want to do:

1. Find all the x in S that are not in T.
2. Find all the x in S or T that are not in both.
3. Find all the x that are in both S and T.

basically #2.

I know how to write this myself by iterating through each map and checking in the other map to see if it exists, but I figure anything built in would be faster than what I would write.

You want the symmetric difference of the two maps. Conveniently enough, the standard library provides you with the set_symmetric_difference template function in <algorithm>.

I'll check it out, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.