It lacks it because nobody added it. Nobody added it because the containers from the STL that the std
library incorporated where designed to be minimal in interface. (Note that std::string
did not come from the STL in the same way).
If you don't mind some strange 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;
// faster than:
// return std::forward<C>(c).count( std::forward<K>(k) ) != 0;
// for multi-meows with lots of duplicates
}
};
template<class K>
containts_t<K> contains( K&& k ) {
return {std::forward<K>(k)};
}
use:
if (some_set->*contains(some_element)) {
}
Basically, you can write extension methods for most C++ std
types using this technique.
It makes a lot more sense to just do this:
if (some_set.count(some_element)) {
}
but I am amused by the extension method method.
The really sad thing is that writing an efficient contains
could be faster on a multimap
or multiset
, as they just have to find one element, while count
has to find each of them and count them.
A multiset containing 1 billion copies of 7 (you know, in case you run out) can have a really slow .count(7)
, but could have a very fast contains(7)
.
With the above extension method, we could make it faster for this case by using lower_bound
, comparing to end
, and then comparing to the element. Doing that for an unordered meow as well as an ordered meow would require fancy SFINAE or container-specific overloads however.
count()
approach is that it does more work than acountains()
would have to do.contains()
which returns abool
would lose valuable information about where the element is in the collection.find()
preserves and returns that information in the form of an iterator, therefore is a better choice for a generic library like STL. (That's not to say that abool contains()
isn't a very nice-to-have or even necessary, though.)contains(set, element)
free function using the public interface of the set. Therefore, the set's interface is functionally complete; adding a convenience method just increases the interface without enabling any additional function, which isn't the C++ way.