std::search_n
Defined in header <algorithm>
|
||
template< class ForwardIt, class Size, class T > ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value ); |
(1) | |
template< class ExecutionPolicy, class ForwardIt, class Size, class T > ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count, const T& value ); |
(2) | (since C++17) |
template< class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, |
(3) | |
template< class ExecutionPolicy, class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count, const T& value, |
(4) | (since C++17) |
Searches the range [first, last)
for the first sequence of count identical elements, each equal to the given value value.
operator==
.p
.policy
. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is trueParameters
first, last | - | the range of elements to examine |
count | - | the length of the sequence to search for |
value | - | the value of the elements to search for |
policy | - | the execution policy to use. See execution policy for details. |
p | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
Type requirements | ||
-ForwardIt must meet the requirements of ForwardIterator .
|
Return value
Iterator to the beginning of the found sequence in the range [first, last)
. If no such sequence is found, last
is returned.
Complexity
At most last - first
applications of the predicate.
Exceptions
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the three standard policies, std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory, std::bad_alloc is thrown.
Possible implementation
First version |
---|
template<class ForwardIt, class Size, class T> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value) { for(; first != last; ++first) { if (!(*first == value)) { continue; } ForwardIt candidate = first; Size cur_count = 0; while (true) { ++cur_count; if (cur_count == count) { // success return candidate; } ++first; if (first == last) { // exhausted the list return last; } if (!(*first == value)) { // too few in a row break; } } } return last; } |
Second version |
template<class ForwardIt, class Size, class T, class BinaryPredicate> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p) { for(; first != last; ++first) { if (!p(*first, value)) { continue; } ForwardIt candidate = first; Size cur_count = 0; while (true) { ++cur_count; if (cur_count == count) { // success return candidate; } ++first; if (first == last) { // exhausted the list return last; } if (!p(*first, value)) { // too few in a row break; } } } return last; } |