std::search_n (3) Linux Manual Page
std::search_n – std::search_n
Synopsis
Defined in header<algorithm>
template <class ForwardIt, class Size, class T>
(until C++ 20)
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T &value);
template <class ForwardIt, class Size, class T>
(since C++ 20)
constexpr ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T &value);
template <class ExecutionPolicy, class ForwardIt, class Size, class T>
(2)(since C++ 17)
ForwardIt search_n(ExecutionPolicy &&policy, ForwardIt first, ForwardIt last, Size count, const T &value);
template <class ForwardIt, class Size, class T, class BinaryPredicate>
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T &value, (1)(until C++ 20)
BinaryPredicate p);
template <class ForwardIt, class Size, class T, class BinaryPredicate>
constexpr ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T &value, (3)(since C++ 20)
BinaryPredicate p);
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)
BinaryPredicate p);
Searches the range [first, last) for the first sequence of count identical elements, each equal to the given value value.
1) Elements are compared using operator==.
3) Elements are compared using the given binary predicate p.
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
Parameters
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 – While the signature does not need to have const &, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value_category (thus, Type1 & is not allowed
Type requirements
–
ForwardIt must meet the requirements of LegacyForwardIterator.
Return value
Iterator to the beginning of the found sequence in the range [first, last). If no such sequence is found, last is returned.
If count is zero or negative, first 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 standard_policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.
Possible implementation
First version
Second version
Example
// Run this code
#include <iostream>
#include <algorithm>
#include <iterator>
template <class Container, class Size, class T>
bool consecutive_values(const Container &c, Size count, const T &v)
{
return std::search_n(std::begin(c), std::end(c), count, v) != std::end(c);
}
int main()
{
const char sequence[] = "1001010100010101001010101";
std::cout << std::boolalpha;
std::cout << "Has 4 consecutive zeros: "
<< consecutive_values(sequence, 4, '0') << '\n';
std::cout << "Has 3 consecutive zeros: "
<< consecutive_values(sequence, 3, '0') << '\n';
}
Output:
See also
find_end (function template)
find
find_if
find_if_not finds the first element satisfying specific criteria
(C++11)
search (function template)
