std::find_end in C++

Last Updated : 21 Aug, 2025
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:
  1. 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.
    
    CPP
    // 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;
    }
    
    Output:
    11
    
  2. 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.
    CPP
    // 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;
    }
    
    Output:
    4
    

Where can it be used ?

  1. 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. CPP
    // C++ program to demonstrate the use of std::find_end
     
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int i, j