在线文档教程
C++
算法 | Algorithm

std::search_n

STD:搜索[医]n

Defined in header
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, BinaryPredicate p (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, BinaryPredicate p (4)(since C++17)

搜索范围[first, last)对于计数相同的第一个元素序列,每个元素都等于给定的值。

1%29个元素的比较operator==...

使用给定的二进制谓词对3%29个元素进行比较。p...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

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. The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type1. The type Type2 must be such that an object of type T can be implicitly converted to Type2. ​

类型要求

---。

返回值

到范围内找到序列的开头的Iterator。[first, last)如果没有找到这样的序列,last会被归还。

复杂性

顶多last - first谓词的应用。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<类向前,类大小,类T>向前搜索[医]n%28 Forwardit First,Forwardit Lest,size count,Const T&value%29{for%28;first%21=Lest;++First%29{if%28%21%28%2A第一==值%29%29{继续;}向前候选=第一;大小为CUR[医]计数=0;而%28真%29{++Cur[医]计数;如果%28 cur[医]计数==计数%29{//成功返回候选人;}+优先;如果%28first==最后%29{//耗尽列表返回最后一个;}如果%28%21%28%2A第一==值%29%29{//行分隔符太少;}}返回最后;}

第二版

模板<类向前,类大小,类T,类二进制预测>向前搜索[医]n%28 Forwardit First,Forwardit Lest,size count,Const T&value,BinaryPredicate p%29{for%28;First%21=Lest;++First%29{if%28%21 p%28%2A首先,值%29%29{继续;}向前推进候选=第一;大小为cur[医]计数=0;而%28真%29{++Cur[医]计数;如果%28 cur[医]计数==计数%29{//成功返回候选人;}+优先;如果%28first==最后%29{//耗尽列表返回最后一个;}如果%28%21 p%28%2A首先,值%29%29{//行分隔符太少;}}返回最后;}

二次

#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'; }

二次

产出:

二次

Has 4 consecutive zeros: false Has 3 consecutive zeros: true

二次

另见

find_endfinds the last sequence of elements in a certain range (function template)
findfind_iffind_if_not (C++11)finds the first element satisfying specific criteria (function template)
searchsearches for a range of elements (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/Search[医]n