Is there an stl way to get a list of values from a map?
i.e, I have:
std::map<A,B> myMap;
and I would like a function that will return just the list of values, i.e, std::list<B> (or set for that matter.
Is there a built-in stl way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A
mapelement is defined as amap::value_type, and the type of it is apair<A,B>.firstis the key andsecondis the value. You can write a functor to extractsecondfrom avalue_type, and copy that in to avector(or alist, or whatever you want.) The best way to do the copying is to usetransform, which does just what its name implies: it takes a value of one type and transforms it to a different type of value.Here’s a complete working example:
EDIT:
If you have a compiler that supports C++0x lambdas, you can eliminate the functor entirely. This is very useful for making code more readable and, arguable, easier to maintain since you don’t end up with dozens of little one-off functors floating around in your codebase. Here’s how you would change the code above to use a lambda: