What about:
template <typename KeyType, typename Collection> bool exists_in(Collection const& haystack, KeyType const& needle) { return std::find(haystack.begin(), haystack.end(), needle) != haystack.end(); } template <typename K, typename V> bool exists_in(std::map<K,V> const& haystack, K const& needle) { return haystack.find(needle) != haystack.end(); }
This makes exists_in work with any standard container via std::find and use the special version for std::map , as it offers a more efficient search alternative. You can add additional specializations as needed (for example, for std::set and others).
D.Shawley May 6, '10 at 15:03 2010-05-06 15:03
source share