How to find all the elements that correspond to a key vector?

advertisements

I have a std::vector<std::string> k; with keys values of witch I seek for. I have a std::map<std::string, void*> table; I search in. I want to get a std::vector<void*> v; of values for all keys that are in k (if such values exist in table, not all keys can be presented in the table and this fact is not important for me, while the fact that there can be much more keys than in k is important). How to find all values that correspond to keys?

Using table[k_element] of table.find(k_element) for each element in k seems like a really bad way to do it also I could not find any algorithm in STL that could do it in a group manner...(


using table[k_element] or table.find(k_element) for each element in k seems like a really bad way

I'm not sure why that feels like a bad way. Even if there were an aglorithm in the STL to do this for you, it would have to either iterate through each value in k -- there's isn't a solution to this problem that's faster than linear time. I think sometimes people think that one line of code is always faster than a loop even if that one line of code calls a function containing a loop.

There are ways to do this in a line with lambdas, map and such, but that's not "better" or faster, just less readable.