Last active
August 29, 2015 14:25
-
-
Save ilpropheta/00cd6b63c64c338174db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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