std::search
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <algorithm> 中定义
|
||
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, |
(1) | |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, |
(2) | |
搜索范围内的第一个序列的元素
[s_first, s_last)
[first, last - (s_last - s_first))
。的第一个版本使用operator==
比较的元素,第二个版本使用给定的二元谓词p
.原文:
Searches for the first subsequence of elements
[s_first, s_last)
in the range [first, last - (s_last - s_first))
. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
.目录 |
[编辑] 参数
first, last | - | 检查的元素
|
s_first, s_last | - | 的元素的范围搜索
|
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. |
类型要求 | ||
-ForwardIt1, ForwardIt2 必须满足 ForwardIterator 的要求。
|
[编辑] 返回值
迭代器的第一子序列的范围内
[s_first, s_last)
[first, last - (s_last - s_first))
开头。如果没有这样的序列被发现,last
是returned.原文:
Iterator to the beginning of first subsequence
[s_first, s_last)
in the range [first, last - (s_last - s_first))
. If no such subsequence is found, last
is returned.如果
[s_first, s_last)
是空的,first
返回。 (C++11 起)原文:
If
[s_first, s_last)
is empty, first
is returned. (C++11 起)[编辑] 复杂度
在最
S*N
比较S = std::distance(s_first, s_last)和N = std::distance(first, last).原文:
At most
S*N
comparisons where S = std::distance(s_first, s_last) and N = std::distance(first, last).[编辑] 可能的实现
版本一 |
---|
template<class ForwardIt1, class ForwardIt2> ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last) { for (; ; ++first) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) { return first; } if (it == last) { return last; } if (!(*it == *s_it)) { break; } } } } |
版本二 |
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate> ForwardIt1 search(ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p) { for (; ; ++first) { ForwardIt1 it = first; for (ForwardIt2 s_it = s_first; ; ++it, ++s_it) { if (s_it == s_last) { return first; } if (it == last) { return last; } if (!p(*it, *s_it)) { break; } } } } |
[编辑] 示例
本章尚未完成 |
[编辑] 另请参阅
查找一定范围内最后出现的元素序列 (函数模板) | |
确定两个元素集合是否是相同的 (函数模板) | |
(C++11) |
查找满足特定条件的第一个元素 (函数模板) |
如果一个区间按字典顺序小于另一个区间,返回true (函数模板) | |
查找两个范围第一个不同元素的位置 (函数模板) | |
在区间中搜索连续一定数目次出现的元素 (函数模板) |