Przejdź do głównej zawartości

std::search_n() algorithm

// (1)
template< class ForwardIt, class Size, class T >
constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value );

// (2)
template< class ForwardIt, class Size, class T, class BinaryPredicate >
constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt, class Size, class T >
ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
Size count, const T& value );

// (4)
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, BinaryPredicate p );

Searches the range for the first sequence of count identical elements, each equal to the given value.

  • (1) Elements are compared using operator==.

  • (2) Elements are compared using the given binary predicate p.

  • (3 - 4) Same as (1 - 2), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to examine.

s_first
s_last

The range of elements to search for.

policy

The execution policy to use. See execution policy for details.

searcher

The searcher encapsulating the search algorithm and the pattern to look for.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it.
  • Must accept all values of type (possibly const) Type1 and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to them.
  • The type Type2 must be such that an object of type T can be implicitly converted to them.

Type requirements

ForwardItLegacyForwardIterator
Size

Must be implicitly convertible to an integral type.

Return value

If count is positive, returns an iterator to the beginning of the first sequence found in the range [first; last).

Each iterator it in the sequence should satisfy the following condition:

  • (1, 3) *it == value
  • (2, 4) p(*it, value)

If no such sequence is found, last is returned.
If count is zero or negative, first is returned.

Complexity

Given N as std::distance(first, last):

  • (1, 3) At most N comparisons with value using operator==.
  • (2, 4) At most N applications of the predicate p.

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

search_n (1)
template<class ForwardIt, class Size, class T>
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value)
{
if (count <= 0)
return first;

for (; first != last; ++first)
{
if (!(*first == value))
continue;

ForwardIt candidate = first;

for (Size cur_count = 1; true; ++cur_count)
{
if (cur_count >= count)
return candidate; // success

++first;
if (first == last)
return last; // exhausted the list

if (!(*first == value))
break; // too few in a row
}
}
return last;
}
search_n (2)
template<class ForwardIt, class Size, class T, class BinaryPredicate>
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value,
BinaryPredicate p)
{
if (count <= 0)
return first;

for (; first != last; ++first)
{
if (!p(*first, value))
continue;

ForwardIt candidate = first;

for (Size cur_count = 1; true; ++cur_count)
{
if (cur_count >= count)
return candidate; // success

++first;
if (first == last)
return last; // exhausted the list

if (!p(*first, value))
break; // too few in a row
}
}
return last;
}

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>

template<class Container, class Size, class T>
[[nodiscard]]
constexpr 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()
{
constexpr char sequence[] = "1001010100010101001010101";

static_assert(consecutive_values(sequence, 3, '0'));

std::cout << std::boolalpha
<< "Has 4 consecutive zeros: "
<< consecutive_values(sequence, 4, '0') << '\n'
<< "Has 3 consecutive zeros: "
<< consecutive_values(sequence, 3, '0') << '\n';
}
Output
Has 4 consecutive zeros: false
Has 3 consecutive zeros: true
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::search_n() algorithm

// (1)
template< class ForwardIt, class Size, class T >
constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value );

// (2)
template< class ForwardIt, class Size, class T, class BinaryPredicate >
constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p );

// (3)
template< class ExecutionPolicy, class ForwardIt, class Size, class T >
ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
Size count, const T& value );

// (4)
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, BinaryPredicate p );

Searches the range for the first sequence of count identical elements, each equal to the given value.

  • (1) Elements are compared using operator==.

  • (2) Elements are compared using the given binary predicate p.

  • (3 - 4) Same as (1 - 2), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  (do C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  (od C++20) is true.

Parameters

first
last

The range of elements to examine.

s_first
s_last

The range of elements to search for.

policy

The execution policy to use. See execution policy for details.

searcher

The searcher encapsulating the search algorithm and the pattern to look for.

p

Binary predicate which returns true if the elements should be treated as equal.

The signature of the function should be equivalent to the following:

bool fun(const Type1& a, const Type2& b);
  • The signature does not need to have const&.
  • The function must not modify the objects passed to it.
  • Must accept all values of type (possibly const) Type1 and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (od C++11))
  • The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to them.
  • The type Type2 must be such that an object of type T can be implicitly converted to them.

Type requirements

ForwardItLegacyForwardIterator
Size

Must be implicitly convertible to an integral type.

Return value

If count is positive, returns an iterator to the beginning of the first sequence found in the range [first; last).

Each iterator it in the sequence should satisfy the following condition:

  • (1, 3) *it == value
  • (2, 4) p(*it, value)

If no such sequence is found, last is returned.
If count is zero or negative, first is returned.

Complexity

Given N as std::distance(first, last):

  • (1, 3) At most N comparisons with value using operator==.
  • (2, 4) At most N applications of the predicate p.

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

search_n (1)
template<class ForwardIt, class Size, class T>
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value)
{
if (count <= 0)
return first;

for (; first != last; ++first)
{
if (!(*first == value))
continue;

ForwardIt candidate = first;

for (Size cur_count = 1; true; ++cur_count)
{
if (cur_count >= count)
return candidate; // success

++first;
if (first == last)
return last; // exhausted the list

if (!(*first == value))
break; // too few in a row
}
}
return last;
}
search_n (2)
template<class ForwardIt, class Size, class T, class BinaryPredicate>
ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value,
BinaryPredicate p)
{
if (count <= 0)
return first;

for (; first != last; ++first)
{
if (!p(*first, value))
continue;

ForwardIt candidate = first;

for (Size cur_count = 1; true; ++cur_count)
{
if (cur_count >= count)
return candidate; // success

++first;
if (first == last)
return last; // exhausted the list

if (!p(*first, value))
break; // too few in a row
}
}
return last;
}

Examples

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>

template<class Container, class Size, class T>
[[nodiscard]]
constexpr 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()
{
constexpr char sequence[] = "1001010100010101001010101";

static_assert(consecutive_values(sequence, 3, '0'));

std::cout << std::boolalpha
<< "Has 4 consecutive zeros: "
<< consecutive_values(sequence, 4, '0') << '\n'
<< "Has 3 consecutive zeros: "
<< consecutive_values(sequence, 3, '0') << '\n';
}
Output
Has 4 consecutive zeros: false
Has 3 consecutive zeros: true
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.