The find_end function in C++ Algorithm is designed to locate the final instance of a specified pattern within a container. This could refer to the concluding appearance of a particular segment of the sequence within the container. The function essentially scans through the range indicated by [first1, last1) to identify the presence of a sequence outlined by [first2, last2). In case the sequence is identified, an iterator pointing to the initial element is provided; if not, the function returns last1.
Syntax
template<class ForwardIterator1, classForwardIterator2>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
Parameter
It serves as a forward iterator pointing to the initial element within the range [first1, last1) with the element itself being part of the range.
It represents a forward iterator pointing to the final element within the range [first1, last1), excluding the element itself from the range.
It represents a forward iterator pointing to the initial element within the specified range [first2, last2), with the element itself being part of the range.
last2 : It serves as a forward iterator pointing to the final element within the range [first2, last2) while excluding the element itself from the range.
pred : It is a binary function that takes two elements as parameters and executes the function's specified operation.
Return value
The function provides an iterator pointing to the initial element of the final instance of [first2, last2) within the specified range [first1, last1). If the sequence is not located, the function will return the value of last1.
Example 1
#include <iostream>
#include <algorithm>
#include <vector>
bool newfunction (int m, int n)
{
return (m==n);
}
int main ()
{
int newints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<int> haystack (newints,newints+10);
int patt1[] = {1,2,3};
std::vector<int>::iterator ti;
ti = std::find_end (haystack.begin(), haystack.end(), patt1, patt1+3);
if (ti!=haystack.end())
std::cout << "patt1 last found at position " << (ti-haystack.begin()) << '\n';
int patt2[] = {4,5,1};
ti = std::find_end (haystack.begin(), haystack.end(), patt2, patt2+3, newfunction);
if (ti!=haystack.end())
std::cout << "patt2 last found at position " << (ti-haystack.begin()) << '\n';
return 0;
}
Output:
patt1 is last found at the position 5
patt2 is last found at the position 3
Example 2
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int>u= {1,3,10,3,10,1,3,3,10,7,8,1,3,10};
vector<int>u1={1,3,10};
vector<int>::iterator ti;
ti=std::find_end(u.begin(),u.end(),u1.begin(),u1.end());
cout<<(ti-u.begin())<<"\n";
return 0;
}
Output:
Complexity
The intricacy of the function is determined by count2*(1+count1-count2. Here countX represents the gap between the initial X and final X.
Data races
Objects in both ranges are accessed.
Exceptions
The function will raise an exception if any of its arguments also raises an exception.