I was wondering, if there is a function of std::map to receive a sub-map defined by a std::vector of keys. So just like the std::map.at(key) function but with a std::vector of keys. I know I can iterate over the map and adding the map pairs to a new map, but I was wondering, if there is a built in function, or maybe a faster way to get a sub-map.
example code:
std::map<std::string, int> map = {{"a", 1}, {"b", 2}, {"c", 3}, {"d", 4}, {"e", 5}};
std::vector<std::string> keys = {"a", "c", "d"};
// using iteration:
std::map<std::string, int> new_map;
for(auto it = map.begin(); it != map.end(); ++it){
if(std::find(keys.begin(), keys.end(), it->first) != keys.end()){
new_map.insert(*it);
}
}
// new_map should be {{"a",1}, {"c",3}, {"d",4}}
// What I am hoping to find is something like:
new_map = map.at(keys);
//resulting in the same new_map {{"a",1}, {"c",3}, {"d",4}}.
Is there such a function or in general a smarter way than iterating over the whole map and using the find function every time (I know I could iterate over the vector, but this doesn't make much of a difference, or does it?).