描述
C++ 函數std::algorithm::find_end()查找元素的最後一次出現。它用二元謂詞用於比較。
聲明
以下是 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);
參數
first1- 將迭代器轉發到第一個序列的初始位置。
last1- 將迭代器轉發到第一個序列的最終位置。
first2- 將迭代器轉發到第二個序列的初始位置。
last2- 將迭代器轉發到第二個序列的最終位置。
pred- 一個二元謂詞,它接受兩個參數並返回 bool。
返回值
返回一個迭代器到最後一次出現的第一個元素(first2,last2)在第一個,最後一個。
異常
如果元素比較或迭代器上的操作拋出異常,則拋出異常。
請注意無效的參數會導致未定義的行為。
時間複雜度
線性。
示例
下麵的例子展示了 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.
相關用法
- C++ Algorithm find_if_not()用法及代碼示例
- C++ Algorithm find_first_of()用法及代碼示例
- C++ Algorithm find_if()用法及代碼示例
- C++ Algorithm find()用法及代碼示例
- C++ Algorithm fill()用法及代碼示例
- C++ Algorithm fill_n()用法及代碼示例
- C++ Algorithm for_each()用法及代碼示例
- C++ Algorithm copy()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm equal()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm adjacent_find()用法及代碼示例
- C++ Algorithm replace_if()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm Library - find_end() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。