Namespaces
Variants
Views
Actions

std::ranges::contains, std::ranges::contains_subrange

From cppreference.com
< cpp‎ | algorithm‎ | ranges
Revision as of 12:07, 27 July 2022 by Parisakhaleghi (Talk | contribs)

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
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*>

constexpr bool ranges::contains(I first, S last, const T& value, Proj proj = {});
(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*>

constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {});
(2) (since C++23)
template<forward_­iterator I1, sentinel_­for<I1> S1, forward_­iterator I2,

         sentinel_­for<I2> S2, class Pred = ranges::equal_to,
         class Proj1 = identity, class Proj2 = identity>
requires indirectly_­comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool ranges::contains_subrange(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},

                            Proj1 proj1 = {}, Proj2 proj2 = {});
(3) (since C++23)
template<forward_­range R1, forward_­range R2, class Pred = ranges::equal_to,

         class Proj1 = identity, class Proj2 = identity>
requires indirectly_­comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr bool ranges::contains_subrange(R1&& r1, R2&& r2, Pred pred = {},

                            Proj1 proj1 = {}, Proj2 proj2 = {});
(4) (since C++23)
1) Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel pairs.
2) Same as (1) but with range objects rather iterator-sentinel pairs.
3) Search-based algorithm that checks whether or not a given range is a subrange of another range with iterator-sentinel pairs.
4) Same as (3) but with range objects rather iterator-sentinel pairs.
Up until C++20, we’ve had to write
std::ranges::find(r, value) != std::ranges::end(r)
to determine if a single value is inside a range, and to check if a range contains a subrange of interest, we use
not std::ranges::search(haystack, needle).empty()
While this is accurate, it isn’t necessarily convenient, and it hardly expresses intent (especially in the latter case). Being able to say std::ranges::contains(r, value) addresses both of these points. Further evidence for the existence of a 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;
(3) and (4):
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

finds the first element satisfying specific criteria
(niebloid)[edit]
searches for the first occurrence of a range of elements
(niebloid)[edit]