std::ranges::contains, std::ranges::contains_subrange
Defined in header <algorithm>
|
||
Call signature |
||
template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity> requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> |
(1) | (since C++23) |
template<input_range R, class T, class Proj = identity> requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*> |
(2) | (since C++23) |
template<forward_iterator I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2, class Pred = ranges::equal_to, |
(3) | (since C++23) |
template<forward_range R1, forward_range R2, class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity> |
(4) | (since C++23) |
std::ranges::find(r, value) != std::ranges::end(r)
not std::ranges::search(haystack, needle).empty()
contains
algorithm is that the STL gave C++98 an obscurely-named contains algorithm called binary_search, and any_of is “contains
with a predicate”, showing that there’s prior art for this contains to be an algorithm.
Contents |
Notes
The interfaces supports both iterator-sentinel pairs and range objects. Due to multi-pass iteration, these algorithms requires both ranges to be forward ranges, and the elements' projections need to be comparable when using the predicate.
ranges::contains_subrange
provides no access to searchers like Boyer-Moore, but that's no more true than for ranges::search.
Parameters
first, last | - | the range of elements to examine |
r | - | the range of the elements to examine |
value | - | value to compare the elements to |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
Return value
(1) and (2):ranges::find(std::move(first), last, value, proj) != last;
first2 == last2 or !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty();
Complexity
At most last
- first
applications of the predicate and projection.
See also
(C++20)(C++20)(C++20) |
finds the first element satisfying specific criteria (niebloid) |
(C++20) |
searches for the first occurrence of a range of elements (niebloid) |