std::search_n
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class ForwardIt, class Size, class T > ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value ); |
(1) | |
template< class ForwardIt, class Size, class T, class BinaryPredicate > ForwardIt1 search_n( ForwardIt first, ForwardIt last, Size count, const T& value, |
(2) | |
为第一序列的计数相同的元件,每个等于给定值的值的范围内搜索
[first, last)
。的第一个版本使用operator==
比较的元素,第二个版本使用给定的二元谓词p
.原文:
Searches the range
[first, last)
for the first sequence of count identical elements, each equal to the given value value. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
.目录 |
[编辑] 参数
first, last | - | 检查的元素
|
count | - | 序列的长度,以搜寻
原文: the length of the sequence to search for |
value | - | 的元素的值,以搜寻
原文: the value of the elements to search for |
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. |
类型要求 | ||
-ForwardIt 必须满足 ForwardIterator 的要求。
|
[编辑] 返回值
iiterator范围在
[first, last)
找到的序列的开头。如果没有发现这样的序列,last
返回.原文:
Iiterator to the beginning of the found sequence in the range
[first, last)
. If no such sequence is found, last
is returned.[编辑] 复杂度
在最
last - first
的谓词中的应用.原文:
At most
last - first
applications of the predicate.[编辑] 可能的实现
版本一 |
---|
template<class ForwardIt, class Size, class T> ForwardIt1 search_n(ForwardIt first, ForwardIt last, Size count, const T& value) { Size curr_count = 0; ForwardIt result, t_last = first; std::advance(t_last, std::distance(first, last) - count + 1); for (; first != t_last; first++) { curr_count = 0; result = first; while (*first == value) { curr_count++; if (curr_count == count) { return result; } ++first; } } return last; } |
版本二 |
template<class ForwardIt, class Size, class T, class BinaryPredicate> ForwardIt1 search_n(ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p) { Size curr_count = 0; ForwardIt result, t_last = first; std::advance(t_last, std::distance(first, last) - count + 1); for (; first != t_last; first++) { curr_count = 0; result = first; while (p(*first == value)) { curr_count++; if (curr_count == count) { return result; } ++first; } } return last; } |
[编辑] 示例
本章尚未完成 |
[编辑] 另请参阅
查找一定范围内最后出现的元素序列 (函数模板) | |
(C++11) |
查找满足特定条件的第一个元素 (函数模板) |
查找一个元素区间 (函数模板) |