I was writing an algorithm this morning and I ran into a curious situation. I have two std::map
s. I want to perform a set intersection on the sets of the keys of each (to find which keys are common to both maps). At some point in the future, I think it's likely I'll also want to perform set subtraction here as well. Luckily, the STL includes functions for both of those operations. The problem is, I can't seem to get a std::set
of the keys out of a std::map
. Is there any way to do this? I'm looking for something that would be this simple, like it is in Java:
std::set<Foo> keys = myMap.getKeySet();
My understanding is that I can't use the std::set_intersection()
function directly on iterators into the maps because the maps expose std::pair
objects instead of just keys. Also, I don't think the map guarantees order. I'm also interested in performing this same operation on a pair of std::multimap
s, if that makes any difference.
EDIT: I forgot to mention initially that due to the age of the compiler I'm forced to use (MSVC++ 6), most of the nifty template tricks that are available in boost can not be used.