This is missing because no one added it. No one added it, because the containers are from the STL, into which the std library is built, where they were minimal in the interface. (Note that std::string did not come from STL in the same way).
If you don't mind some weird syntax, you can fake it:
template<class K> struct contains_t { K&& k; template<class C> friend bool operator->*( C&& c, contains_t&& ) { auto range = std::forward<C>(c).equal_range(std::forward<K>(k)); return range.first != range.second;
using:
if (some_set->*contains(some_element)) { }
Basically, you can write extension methods for most types of C ++ std using this technique.
There is much more sense for this:
if (some_set.count(some_element)) { }
but the extension method method surprises me.
The sad thing is that writing effective contains can be faster on multimap or multiset , because they just have to find one element, and count must find each of them and count them.
A multiset containing 1 billion instances of 7 (you know, in case you are done) can have a very slow .count(7) , but it can have a very fast contains(7) .
Using the aforementioned extension method, we could do it faster for this case, using lower_bound , compared to end , and then comparing it to the element. For this, disordered meow as well as ordered meow will require fantastic SFINAE or container-specific overloads.
Yakk - Adam Nevraumont Mar 01 '17 at 18:54 2017-03-01 18:54
source share