std::find_end in C++
std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
It is similar to std::search in such a way that in std::search , we look for the first occurrence of a sub-sequence inside another container, whereas in std::find_end, we look for the last occurrence of such sub-sequence, and returns an iterator to the first element if such sub-sequence is found.
It can be used in two ways as shown below:
-
Comparing elements using ==:
Syntax:
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. Return Value: 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>usingnamespacestd;intmain(){// Defining first containervector<int>v = {1, 3, 10, 3, 10, 1, 3, 3, 10, 7, 8,1, 3, 10};// Defining second containervector<int>v1 = {1, 3, 10};vector<int>::iterator ip;// Using std::find_endip = std::find_end(v.begin(), v.end(), v1.begin(),v1.end());// Displaying the index where the last common occurrence// beginscout << (ip - v.begin()) <<"\n";return0;}chevron_rightfilter_noneOutput:
11
- By comparing using a pre-defined function:
Syntax:
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. Return Value: 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>usingnamespacestd;// Defining the BinaryFunctionboolPred (inta,intb){return(a == b);}intmain(){// Defining first containervector<int>v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i;// Defining second containervector<int>v1 = {13, 15};vector<int>::iterator ip;// Using std::find_endip = std::find_end(v.begin(), v.end(), v1.begin(),v1.end(), Pred);// Displaying the index where the last common occurrence// beginscout << (ip - v.begin()) <<"\n";return0;}chevron_rightfilter_noneOutput:
4
Where can it be used ?
- To search from the end: It is the reverse variant of std::search, i.e., std::search searches for a sub-sequence from the beginning of the list , such that it can return the first occurrence of that subsequence.
On the other hand std::find_end can be used if we want to search for a sub-sequence from the end of the list, which will automatically be the last occurrence of any subsequence inside a container.
(You are not alone if you are thinking why it is called std::find_end and not std::search_end!!!)So, it is a possible replacement of std::search, if searching has to be done from the end.
// C++ program to demonstrate the use of std::find_end#include <iostream>#include <vector>#include <algorithm>usingnamespacestd;intmain(){inti, j;// Declaring the sequence to be searched intovector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 3, 4, 5 };// Declaring the subsequence to be searched forvector<int> v2 = {3, 4};// Declaring an iterator for storing the returning pointervector<int>::iterator i1;// Using std::search to find the first occurrence of v2i1 = std::search(v1.begin(), v1.end(), v2.begin(),v2.end());// checking if iterator i1 contains end pointer of v1 or// notif(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 v2i1 = std::find_end(v1.begin(), v1.end(), v2.begin(),v2.end());// checking if iterator i1 contains end pointer of v1 or//notif(i1 != v1.end()) {cout <<"\nvector2 is present lastly at index "<< (i1 - v1.begin());}else{cout <<"vector2 is not present in vector1";}return0;}chevron_rightfilter_noneOutput:
vector2 is present firstly at index 2 vector2 is present lastly at index 7
-
To find last occurrence of element satisfying a condition: Since, std::find_end starts searching from the end, so we can use this fact as well as manipulate the BinaryFunction to solve questions which demand us to find the last occurrence of anything (like last odd number, last even number, and so on).
// C++ program to find the last occurrence of an odd// and even number#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;// Defining the Predicate Function to find the last occurrence// of an odd numberboolpred(inta,intb){if(a % b != 0) {return1;}else{return0;}}// Defining the Predicate Function to find the last occurrence// of an even numberboolpred1(inta,intb){if(a % b == 0) {return1;}else{return0;}}intmain(){// Defining a vectorvector<int>v1 = {1, 3, 4, 5, 6, 7, 8, 10};// Declaring a sub-sequencevector<int>v2 = {2};// Using std::find_end to find the last occurrence of an// odd numbervector<int>::iterator ip;ip = std::find_end(v1.begin(), v1.end(), v2.begin(),v2.end(), pred);// Displaying the index where the last odd number occurredcout <<"Last odd no. occurs at "<< (ip - v1.begin());// Using std::find_end to find the last occurrence of an// even numberip = std::find_end(v1.begin(), v1.end(), v2.begin(),v2.end(), pred1);// Displaying the index where the last even number occurredcout <<"\nLast even no. occurs at "<< (ip - v1.begin());return0;}chevron_rightfilter_noneOutput:
Last odd no. occurs at 5 Last even no. occurs at 7
Explanation of code: Here, we have manipulated Binary Function such that it searches whether the element in the first container is a multiple of elements in the second container or not, and in second container we have stored 2, so with the help of this we are able to find the last occurrence of an odd or even number.
Related Articles:
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- C++ | asm declaration
- Pointers and References in C++
- Strings in C++ and How to Create them?
- Enum Classes in C++ and Their Advantage over Enum DataType
- Deque vs Vector in C++ STL
- C++ Programming Basics
- Introduction to C++ Programming Language
- ios manipulators noshowpos() function in C++
- ios rdstate() function in C++ with Examples
- ios operator !() function in C++ with Examples
- ios operator() function in C++ with Examples
- ios clear() function in C++ with Examples
- ios good() function in C++ with Examples
- ios setstate() function in C++ with Examples
- ios eof() function in C++ with Examples
Improved By : Akanksha_Rai



