原文地址:http://www.cplusplus.com/reference/algorithm/find_end/
function template
<algorithm>
std::find_end
equality (1)
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
|
predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
|
Find last subsequence in range
Searches the range[first1,last1)
for the last occurrence of the sequence defined by[first2,last2)
, and returns an iterator to its first element, orlast1if no occurrences are found.
查找在[first1,last1)中,与[first2,last2)范围内的元素最后一次匹配时出现的位置,并返回一个指向[frist1,last1)中匹配的第一个元素的迭代器,如果没有,则返回last1.
例子:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void findend()
{
vector<int> v1{1,2,3,4,5,6,3,4,9,8};
int arr[]={9,8};
array<double,2> ai{3,4};
cout<<"v1=";
for(int &i:v1)
cout<<i<<" ";
cout<<endl;
cout<<"arr=";
for(int &i:arr)
cout<<i<<" ";
cout<<endl<<"ai=";
for(double &i:ai)
cout<<i<<" ";
cout<<endl<<"auto it=find_end(v1.begin(),v1.end(),arr,arr+2);"<<endl;
auto it=find_end(v1.begin(),v1.end(),arr,arr+2);
cout<<"*(it-1)="<<*(it-1)<<endl;
cout<<"*it="<<*it<<endl;
cout<<"*(it+1)="<<*(it+1)<<endl;
cout<<endl<<"auto it2=find_end(v1.begin(),v1.end(),ai.begin(),ai.end());"<<endl;
auto it2=find_end(v1.begin(),v1.end(),ai.begin(),ai.end());
cout<<"*(it2-1)="<<*(it2-1)<<endl;
cout<<"*it2="<<*it2<<endl;
cout<<"*(it2+1)="<<*(it2+1)<<endl;
}
运行截图:
The elements in both ranges are compared sequentially usingoperator==
(orpred, in version(2)): A subsequence of[first1,last1)
is considered a match only when this istrue
forallthe
elements of[first2,last2)
.
使用operator==进行比较,只有在[first1,last1)范围中中匹配的元素满足[first2,last2)都相等时,才算是匹配了。
This function returns the last of such occurrences. For an algorithm that returns the first instead, seesearch.
函数返回匹配出现的最后一次的位置。要返回第一次出现的位置,使用search.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return last1; // specified in C++11
ForwardIterator1 ret = last1;
while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version (2)
++it1; ++it2;
if (it2==last2) { ret=first1; break; }
if (it1==last1) return ret;
}
++first1;
}
return ret;
}
|
|
Parameters
first1, last1
Forward iteratorsto the initial and final positions of the searched
sequence. The range used is[first1,last1)
, which contains all the elements betweenfirst1andlast1, including the element pointed byfirst1but not the element pointed bylast1.
要查找的范围。
first2, last2
Forward iteratorsto the initial and final positions of the sequence
to be searched for. The range used is[first2,last2)
.
For(1), the elements in both ranges shall be of types comparable usingoperator==
(with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).
要匹配的范围。
pred
Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible tobool
. The returned value indicates whether
the elements are considered to match in the context of this function.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
一个二元函数。
Return value
An iterator to the first element of the last occurrence of[first2,last2)
in[first1,last1)
.
If the sequence is not found, the function returnslast1.
最后一次匹配出现的位置。返回匹配位置的首元素。
如果找不到匹配,就返回last1.
If[first2,last2)
is an empty range, the function returnslast1.
如果[first2,last2)是一个空的序列,返回last!.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// find_end example
#include <iostream> // std::cout
#include <algorithm> // std::find_end
#include <vector> // std::vector
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<int> haystack (myints,myints+10);
int needle1[] = {1,2,3};
// using default comparison:
std::vector<int>::iterator it;
it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);
if (it!=haystack.end())
std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';
int needle2[] = {4,5,1};
// using predicate comparison:
it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);
if (it!=haystack.end())
std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';
return 0;
}
|
|
Output:
needle1 found at position 5
needle2 found at position 3
|
Complexity
Up to linear incount2*(1+count1-count2)
, wherecountXis thedistancebetweenfirstXandlastX:
Compares elements until the last matching subsequence is found.
Data races
Some (or all) of the objects in both ranges are accessed (possibly more than once).
Exceptions
Throws if any element comparison (or call topred) throws or if any of the operations on iterators throws.
Note that invalid arguments causeundefined behavior.
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-12
于GDUT
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++读取STL文件,输出所有三角形的顶点坐标
STL课件_C++_课件stl_c++课件_源码.zip
STL算法(for_each/transform)
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
STL学习教程,
20121018_在成员函数中使用STL的find_if函数1
STL 最新源代码
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
SGI STL是由SGI公司开发的STL库.这份STL可以用在VC上.
vc显示stl,读取顶点坐标,并显示在独立窗口中
在MATLAB中完成STL数据的渲染,用PATCH函数完成加颜色和亮度等功能
Mastering_the_C 17_STL Mastering_the_C 17_STL Mastering_the_C 17_STL
最新的STL源码,最新的STL源码,最新的STL源码
读取、显示STL格式的3D文件,可对二进制的STL文件进行读取、显示。显示模式可以选择有网格、无网格等。
是find_if的使用方法,但是文件名是Test_find,懒得改成Test_find_if了。代码没什么注释,既然会用STL那就一定看得明白。