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

std::find_end

STD:找到[医]端部

Defined in header
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last (1)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_end( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last (2)(since C++17)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > ForwardIt1 find_end( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p (4)(since C++17)

搜索元素的最后一个子序列。[s_first, s_last)在范围内[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
s_first, s_last-the range of 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 types Type1 and Type2 must be such that objects of types ForwardIt1 and ForwardIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ​

类型要求

-前进1必须符合先行者的要求。

-前进2必须符合先行者的要求。

返回值

到最后一个子序列的开头[s_first, s_last)在范围内[first, last)...

If no such subsequence is found, last is returned.(until C++11)
If [s_first, s_last) is empty or if no such subsequence is found, last is returned.(since C++11)

复杂性

充其量S*(N-S+1)比较S = distance(s_first, s_last)N = distance(first, last)...

例外

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

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

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

可能的实施

第一版

*。

模板<类ForwardIt 1,类ForwardIt 2>ForwardIt 1查找[医]结束%28ForwardIt1优先,ForwardIt 1最后,ForwardIt 2[医]第一,前进2[医]最后%29(如果%28 s)[医]第一=s[医]最后%29返回最后;ForwardIt 1结果=最后;而%281%29{Forwardit 1新建[医]结果=std::搜索%281,最后,s[医]第一,s[医]最后%29;如果%28新[医]结果==最后%29{返回结果;}[医]结果;第一=结果;++第一;}返回结果;}

第二版

模板<类ForwardIt 1,类ForwardIt 2,类BinaryPredicate>ForwardIt 1查找[医]结束%28ForwardIt1优先,ForwardIt 1最后,ForwardIt 2[医]第一,前进2[医]最后,二进制预测p%29{if%28s[医]第一=s[医]最后%29返回最后;ForwardIt 1结果=最后;而%281%29{Forwardit 1新建[医]结果=std::搜索%281,最后,s[医]第一,s[医]最后,p%29;if%28新[医]结果==最后%29{返回结果;}[医]结果;第一=结果;++第一;}返回结果;}

下面的代码使用find_end()搜索两个不同的数字序列。

二次

#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}; std::vector<int>::iterator result; std::vector<int> t1{1, 2, 3}; result = std::find_end(v.begin(), v.end(), t1.begin(), t1.end() if (result == v.end()) { std::cout << "subsequence not found\n"; } else { std::cout << "last subsequence is at: " << std::distance(v.begin(), result) << "\n"; } std::vector<int> t2{4, 5, 6}; result = std::find_end(v.begin(), v.end(), t2.begin(), t2.end() if (result == v.end()) { std::cout << "subsequence not found\n"; } else { std::cout << "last subsequence is at: " << std::distance(v.begin(), result) << "\n"; } }

二次

产出:

二次

last subsequence is at: 8 subsequence not found

二次

另见

searchsearches for a range of elements (function template)
includesreturns true if one set is a subset of another (function template)
adjacent_findfinds the first two adjacent items that are equal (or satisfy a given predicate) (function template)
findfind_iffind_if_not (C++11)finds the first element satisfying specific criteria (function template)
find_first_ofsearches for any one of a set of elements (function template)
search_nsearches for a number consecutive copies of an element in a range (function template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/Algorithm/find[医]端部