First I will give a specific case, and the I would like to see if it can be applied to a general problem.
Say I have map. And I want to get all the keys meeting a certain criteria. For example all keys that contain ‘COL’. My naive implementation will be
template<typename T> void Filter (map<string, T> & m, std:set<string> & result, const std::string& condition) { for(map<string,string> iter=m.begin();iter!m.end();iter++) { std::string key=iter->first; size_t found=key.find(condition); if (found!=string::npos) result.insert(key); } }
what is the good way to implement this?
Also, what is a good way to implement general problem when I want to filter map using algos?
I think your solution is rather good: it is clear, and except if you can ‘guess’ hash values based on the condition, I don’t think you could be much more performant. However, you could change your function to make it more generic:
Your example can then be writen using a functor as predicate:
The call to filter will then look like this: