map<T,Z> m= ...;
vector<T> v;
v.reserve(m.size);
for(map<T,Z>::iterator it=m.begin();it!=m.end();++it)
{
v.push_back(it->first);
}
Is there a nicer 1-line version using some STL function(s)?
edit: not using c++11!
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.
Portable:
I think some implementations of STL have a non-standard extension called
select1st, which is the equivalent ofSelectKeyshown here. As K-Ballo pointed out in the comments, there’s also a TR1 version. I like the explicitly-named version as it’s easier to see what’s going on.Since there’s no need for state, you can get away with slightly less boilerplate by using an actual function rather than a functor:
If you could use C++11, you could use a lambda, which keeps the selection code close to where it’s used:
or even range-based for-loop, which is probably the most elegant and readable: