Skip to content

Instantly share code, notes, and snippets.

@ilpropheta
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilpropheta/00cd6b63c64c338174db to your computer and use it in GitHub Desktop.
Save ilpropheta/00cd6b63c64c338174db to your computer and use it in GitHub Desktop.
// canonical way
template<typename MapType>
auto map_vals(const MapType& m)
{
vector<typename MapType::mapped_type> res;
res.reserve(m.size());
transform(begin(m), end(m), back_inserter(res), [](const auto& p) {
return p.second;
});
return res;
}
// Less efficient than the canonical one but more concise :)
template<typename MapType>
auto map_vals(const MapType& m)
{
return accumulate(begin(m), end(m), vector<typename MapType::mapped_type>(), [](auto& values, const auto& curr){
values.emplace_back(curr.second);
return move(values);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment