Difference between revisions of "cpp/algorithm/ranges/contains"
m (minor cleanups) |
m (→Notes: +link to Searchers, +FTM, reworded a bit.) |
||
Line 71: | Line 71: | ||
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. | 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. | ||
− | {{tt|ranges::contains_subrange}} provides no access to | + | {{tt|ranges::contains_subrange}}, same as {{lc|ranges::search}}, but as opposed to {{lc|std::search}}, provides no access to {{named req|Searcher}}s (such as [[cpp/utility/functional#Searchers|Boyer-Moore]]). |
+ | |||
+ | {{feature test macro|__cpp_lib_ranges_contains}} | ||
=== Example === | === Example === |
Revision as of 11:47, 28 July 2022
Defined in header <algorithm>
|
||
Call signature |
||
template< std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity > |
(1) | (since C++23) |
template< ranges::input_range R, class T, class Proj = std::identity > requires std::indirect_binary_predicate<ranges::equal_to, |
(2) | (since C++23) |
template< std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, std::sentinel_for<I2> S2, |
(3) | (since C++23) |
template< ranges::forward_range R1, ranges::forward_range R2, class Pred = ranges::equal_to, |
(4) | (since C++23) |
r
as the source range, as if using ranges::begin(r) as first
and ranges::end(r) as last
.r1
as the first source range and r2
as the second source range, as if using ranges::begin(r1) as first1
, ranges::end(r1) as last1
, ranges::begin(r2) as first2
, and ranges::end(r2) as last2
.The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Up until C++20, we’ve had to writestd::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 |
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
(3) and (4):
- first2 == last2 || !ranges::search(first1, last1, first2, last2, pred, proj1, proj2).empty()
Complexity
At most last
- first
applications of the predicate and projection.
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
, same as ranges::search, but as opposed to std::search, provides no access to Searchers (such as Boyer-Moore).
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_ranges_contains |
Example
This section is incomplete Reason: example |
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) |