std::find_end用於查找容器內子序列的最後一次出現。它在範圍[first1,last1)中搜索由[first2,last2)定義的序列的最後一次出現,然後將迭代器返回到其第一個元素,如果沒有發現,則返回last1。
它與std::search相似,在std::search中,我們尋找另一個容器中子序列的第一個出現,而在std::find_end中,我們尋找該子序列的最後一個出現-sequence,如果找到了子序列,則將迭代器返回第一個元素。
可以按以下兩種方式使用它:
-
使用==比較元素:
用法:
Template ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); first1: Forward iterator to the first element in the first range. last1: Forward iterator to the last element in the first range. first2: Forward iterator to the first element in the second range. last2: Forward iterator to the last element in the second range. 返回值:It returns an iterator to the first element of the last occurrence of [first2,last2) in [first1,last1). If the sequence is not found or [first2,last2) is empty, the function returns last1.
// C++ program to demonstrate the use of std::find_end #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { // Defining first container vector<int>v = {1, 3, 10, 3, 10, 1, 3, 3, 10, 7, 8, 1, 3, 10}; // Defining second container vector<int>v1 = {1, 3, 10}; vector<int>::iterator ip; // Using std::find_end ip = std::find_end(v.begin(), v.end(), v1.begin(), v1.end()); // Displaying the index where the last common occurrence // begins cout << (ip - v.begin()) << "\n"; return 0; }
輸出:
11
- 通過使用預定義函數進行比較:
用法:
Template ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); Here, first1, last1, first2, and last2 are the same as the previous case. 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 to bool. The value returned 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. 返回值:It returns an iterator to the first element of the last occurrence of [first2,last2) in [first1,last1). If the sequence is not found or [first2,last2) is empty, the function returns last1.
// C++ program to demonstrate the use of std::find_end #include<iostream> #include<vector> #include<algorithm> using namespace std; // Defining the BinaryFunction bool Pred (int a, int b) { return (a == b); } int main() { // Defining first container vector<int>v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i; // Defining second container vector<int>v1 = {13, 15}; vector<int>::iterator ip; // Using std::find_end ip = std::find_end(v.begin(), v.end(), v1.begin(), v1.end(), Pred); // Displaying the index where the last common occurrence // begins cout << (ip - v.begin()) << "\n"; return 0; }
輸出:
4
Where can it be used ?
- 從頭開始搜索:它是std::search的相反變體,即std::search從列表的開頭搜索子序列,以便它可以返回該子序列的第一個匹配項。
另一方麵,如果我們要從列表的末尾搜索子序列,則可以使用std::find_end,這將自動成為容器中任何子序列的最後一次出現。 (如果您正在考慮為什麽將其稱為std::find_end而不是std::search_end,您並不孤單!)
因此,如果必須從頭開始進行搜索,則可以替換std::search。
// C++ program to demonstrate the use of std::find_end #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int i, j; // Declaring the sequence to be searched into vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 3, 4, 5 }; // Declaring the subsequence to be searched for vector<int> v2 = {3, 4}; // Declaring an iterator for storing the returning pointer vector<int>::iterator i1; // Using std::search to find the first occurrence of v2 i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end()); // checking if iterator i1 contains end pointer of v1 or // not if (i1 != v1.end()) { cout << "vector2 is present firstly at index " << (i1 - v1.begin()); } else { cout << "vector2 is not present in vector1"; } // Using std::find_end to find the last occurrence of v2 i1 = std::find_end(v1.begin(), v1.end(), v2.begin(), v2.end()); // checking if iterator i1 contains end pointer of v1 or //not if (i1 != v1.end()) { cout << "\nvector2 is present lastly at index " << (i1 - v1.begin()); } else { cout << "vector2 is not present in vector1"; } return 0; }
輸出:
vector2 is present firstly at index 2 vector2 is present lastly at index 7
-
要查找滿足條件的元素的最後一次出現:由於std::find_end從頭開始搜索,因此我們可以使用此事實以及操縱BinaryFunction來解決需要我們找到任何事物的最後出現的問題(例如,最後一個奇數,最後一個偶數,等等) )。
// C++ program to find the last occurrence of an odd // and even number #include<iostream> #include<vector> #include<algorithm> using namespace std; // Defining the Predicate Function to find the last occurrence // of an odd number bool pred( int a, int b) { if (a % b != 0) { return 1; } else { return 0; } } // Defining the Predicate Function to find the last occurrence // of an even number bool pred1( int a, int b) { if (a % b == 0) { return 1; } else { return 0; } } int main() { // Defining a vector vector<int>v1 = {1, 3, 4, 5, 6, 7, 8, 10}; // Declaring a sub-sequence vector<int>v2 = {2}; // Using std::find_end to find the last occurrence of an // odd number vector<int>::iterator ip; ip = std::find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), pred); // Displaying the index where the last odd number occurred cout << "Last odd no. occurs at " << (ip - v1.begin()); // Using std::find_end to find the last occurrence of an // even number ip = std::find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), pred1); // Displaying the index where the last even number occurred cout << "\nLast even no. occurs at " << (ip - v1.begin()); return 0; }
輸出:
Last odd no. occurs at 5 Last even no. occurs at 7
代碼說明:在這裏,我們對Binary Function進行了操縱,以便它搜索第一個容器中的元素是否是第二個容器中的多個元素,並且在第二個容器中我們存儲了2,因此借助此函數,我們能夠找到最後一次出現的奇數或偶數。
相關文章:
相關用法
注:本文由純淨天空篩選整理自 std::find_end in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。