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.
std::vector<std::pair<const Key, Val>> v(map.begin(), map.end());
which should give you a vector of key-value pairs. – Raymondvector<string> vs = um.enum_keys();
supposed to signify? – Creativeenum_keys()
doesn't exist, but that is the 'format' you want it in, so I thought I'd clarify. – Mliter<key> get_keys()
function. – Footway