algorithm::find_end()
描述 (Description)
C ++函数std::algorithm::find_end()查找元素的最后一次出现。 它使用二元谓词进行比较。
声明 (Declaration)
以下是std :: algorithm :: find_end()函数形式std :: algorithm头的声明。
C++98
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);
参数 (Parameters)
first1 - 将迭代器转发到第一个序列的初始位置。
last1 - 将迭代器转发到第一个序列的最终位置。
first2 - 将迭代器转发到第二个序列的初始位置。
last2 - 将迭代器转发到第二个序列的最终位置。
pred - 一个二元谓词,它接受两个参数并返回bool。
返回值
返回first1,last1中最后一次出现的第一个元素(first2,last2)的迭代器 。
异常 (Exceptions)
如果元素比较或迭代器上的操作抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
线性。
例子 (Example)
以下示例显示了std :: algorithm :: find_end()函数的用法。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool binary_pred(int a, int b) {
return (a==b);
}
int main(void) {
vector<int> v1 = {1, 2, 1, 2, 1, 2};
vector<int> v2 = {1, 2};
auto result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), binary_pred);
if (result != v1.end())
cout << "Last sequence found at location "
<< distance(v1.begin(), result) << endl;
v2 = {1, 3};
result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), binary_pred);
if (result == v1.end())
cout << "Sequence doesn't present in vector." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Last sequence found at location 4
Sequence doesn't present in vector.