What is the most efficient way of obtaining lists (as a vector
) of the keys and values from an unordered_map
?
For concreteness, suppose the map in question is a unordered_map<string, double>
.
I'd then like to obtain the keys as a vector<string>
, and the values as a vector<double>
.
unordered_map<string, double> um;
vector<string> vs = um.enum_keys();
vector<double> vd = um.enum_values();
I can just iterate across the map and collect the result, but is there a more efficient method? It would be nice to have a method that also works for regular map, since I might switch to that.
See Question&Answers more detail:os