C++ find_end()
C++ 算法函数find_end()
C++ 算法find_end()函数搜索容器中模式的最后一次出现,或说容器中一小部分的最后一次出现容器中的顺序。它基本上在[first1,last1)指定的范围内搜索[first2,last2)定义的序列的出现。如果找到该匹配项,则返回第一个元素的迭代器,否则返回last1、
语法
template<class ForwardIterator1, classForwardIterator2>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
参数
first1 : 它是范围[first1,last1)中第一个元素的正向迭代器,其中元素本身包含在其中
last1 : 它是范围[first1,last1)中last元素的前向迭代器,其中元素本身不包含在范围中。
first2 : 它是范围[first2,last2)中第一个元素的正向迭代器,其中元素本身包含在范围中。
last2 : 它是范围[first2,last2)中last元素的正向迭代器,其中元素本身不包含在范围中。
pred : 它是一个二进制函数,接受两个元素作为参数并执行该函数设计的任务。
返回值
该函数将迭代器返回到[first1,last1)范围内最后一次出现的[first2,last2)的第一个元素。如果找不到序列,则该函数返回last1值。
示例1
#include <iostream>
#include <algorithm>
#include <vector>
bool newfunction (int m, int n)
{
return (m==n);
}
int main ()
{
int newints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<int> haystack (newints,newints+10);
int patt1[] = {1,2,3};
std::vector<int>::iterator ti;
ti = std::find_end (haystack.begin(), haystack.end(), patt1, patt1+3);
if (ti!=haystack.end())
std::cout << "patt1 last found at position " << (ti-haystack.begin()) << '\n';
int patt2[] = {4,5,1};
ti = std::find_end (haystack.begin(), haystack.end(), patt2, patt2+3, newfunction);
if (ti!=haystack.end())
std::cout << "patt2 last found at position " << (ti-haystack.begin()) << '\n';
return 0;
}
输出:
patt1 is last found at the position 5
patt2 is last found at the position 3
示例2
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int>u= {1,3,10,3,10,1,3,3,10,7,8,1,3,10};
vector<int>u1={1,3,10};
vector<int>::iterator ti;
ti=std::find_end(u.begin(),u.end(),u1.begin(),u1.end());
cout<<(ti-u.begin())<<"\n";
return 0;
}
输出:
复杂度
函数的复杂度由count2 *(1 + count1-count2指定。此处countX指定firstX与lastX之间的距离。
数据竞争
访问两个范围中的对象。
异常
该函数将引发异常(如果有)的参数抛出一个。