std::find_end in C++
Last Updated :
25 Apr, 2019
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.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector< int >v = {1, 3, 10, 3, 10, 1, 3, 3, 10, 7, 8,
1, 3, 10};
vector< int >v1 = {1, 3, 10};
vector< int >::iterator ip;
ip = std::find_end(v.begin(), v.end(), v1.begin(),
v1.end());
cout << (ip - v.begin()) << "\n" ;
return 0;
}
|
Output:
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.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool Pred ( int a, int b)
{
return (a == b);
}
int main()
{
vector< int >v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i;
vector< int >v1 = {13, 15};
vector< int >::iterator ip;
ip = std::find_end(v.begin(), v.end(), v1.begin(),
v1.end(), Pred);
cout << (ip - v.begin()) << "\n" ;
return 0;
}
|
Output:
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.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int i, j;
vector< int > v1 = { 1, 2, 3, 4, 5, 6, 7, 3, 4, 5 };
vector< int > v2 = {3, 4};
vector< int >::iterator i1;
i1 = std::search(v1.begin(), v1.end(), v2.begin(),
v2.end());
if (i1 != v1.end()) {
cout << "vector2 is present firstly at index "
<< (i1 - v1.begin());
} else {
cout << "vector2 is not present in vector1" ;
}
i1 = std::find_end(v1.begin(), v1.end(), v2.begin(),
v2.end());
if (i1 != v1.end()) {
cout << "\nvector2 is present lastly at index "
<< (i1 - v1.begin());
} else {
cout << "vector2 is not present in vector1" ;
}
return 0;
}
|
Output:
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).
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool pred( int a, int b)
{
if (a % b != 0) {
return 1;
} else {
return 0;
}
}
bool pred1( int a, int b)
{
if (a % b == 0) {
return 1;
} else {
return 0;
}
}
int main()
{
vector< int >v1 = {1, 3, 4, 5, 6, 7, 8, 10};
vector< int >v2 = {2};
vector< int >::iterator ip;
ip = std::find_end(v1.begin(), v1.end(), v2.begin(),
v2.end(), pred);
cout << "Last odd no. occurs at " << (ip - v1.begin());
ip = std::find_end(v1.begin(), v1.end(), v2.begin(),
v2.end(), pred1);
cout << "\nLast even no. occurs at " << (ip - v1.begin());
return 0;
}
|
Output:
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:
Share your thoughts in the comments
Please Login to comment...