What is the find_end() function in C++?
The find_end() function in C++ helps to find for a sequence of elements that is present at the last inside an array/vector. This function is available in the <algorithm.h> header file.
Parameters
The find_end() function accepts the following parameters:
-
first1, last1: These are the iterators pointing to the initial and final position of the array/vector where the search operation needs to be performed. -
first2, last2: These are the iterators pointing to the initial and final position of the array/vector that needs to be searched. -
pred: This is an optional parameter that specifies a binary function returning a boolean value which indicates how to perform the search.
Return value
The find_end() function returns an iterator that points to the last occurrence of the sequence in the range [first2, last2) in the sequence [first1, last1). If the value is not found, then it returns an iterator to the last1 of the array or vector.
Code
Let’s look at the code now.
#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () {vector<int> vec = {1,2,3,4,5,1,2,3,4,5};int seq[] = {1,2,3};auto it = find_end(vec.begin(), vec.end(), seq, seq + 3);if (it!=vec.end())cout << "Sequence found at last position " << (it-vec.begin());elsecout << "Sequence not found";return 0;}
Explanation:
- From lines 1 to 3, we import the required libraries.
- In line 7, we create a vector with some integer values.
- In line 9, we define the sequence that we want to search in the vector.
- In line 10, we call the
find_end()function and pass all the required parameters. - In line 12, we check whether the sequence is found or not. If the sequence is not found, then the value of
itwill point to the end of the vector. - In line 13, we print the index from where the sequence starts, and this index will point to the last occurrence of the sequence.
- In line 15, we print if the sequence is not found.
Now, let’s look at the code in which we will use a binary predicate to perform the find operation.
#include <iostream>#include <algorithm>#include <vector>using namespace std;bool pred (int i, int j) {return (i==j);}int main () {vector<int> vec = {1,2,3,4,5,1,2,3,4,5};int seq[] = {1,2,4};auto it = find_end(vec.begin(), vec.end(), seq, seq + 3, pred);if (it!=vec.end())cout << "Sequence found at last position " << (it-vec.begin());elsecout << "Sequence not found";return 0;}
Explanation:
- From lines 1 to 3, we import the required libraries.
- From lines 6 to 8, we create a binary function that accepts two integers and returns a boolean value.
- From lines 11, we create a vector with some integer values.
- In line 13, we define the sequence that we want to search in the vector.
- In line 14, we call the
find_end()function and pass all the required parameters. - In line 16, we check on whether the sequence is found or not. If the sequence is not found, then the value of
itwill point to the end of the vector. - In line 17, we print the index from where the sequence starts, and this index will point to the last occurrence of the sequence.
- In line 19, we print if the sequence is not found.
So, in this way, we can use the find_end() function in C++.