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:

  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.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
    }

    chevron_right

    
    

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate the use of std::find_end
       
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int i, j;
       
        // Declaring the sequence to be searched into
        vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 3, 4, 5 };
       
        // Declaring the subsequence to be searched for
        vector<int> v2 = {3, 4};
       
        // Declaring an iterator for storing the returning pointer
        vector<int>::iterator i1;
       
        // Using std::search to find the first occurrence of v2
        i1 = std::search(v1.begin(), v1.end(), v2.begin(), 
                         v2.end());
       
        // checking if iterator i1 contains end pointer of v1 or
        // not
        if (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 v2
        i1 = std::find_end(v1.begin(), v1.end(), v2.begin(), 
                           v2.end());
       
        // checking if iterator i1 contains end pointer of v1 or 
        //not
        if (i1 != v1.end()) {
            cout << "\nvector2 is present lastly at index " 
                 << (i1 - v1.begin());
        } else {
            cout << "vector2 is not present in vector1";
        }
        return 0;
    }

    chevron_right

    
    

    Output:

    vector2 is present firstly at index 2
    vector2 is present lastly at index 7
    
  2. 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).

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to find the last occurrence of an odd
    // and even number
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
      
    // Defining the Predicate Function to find the last occurrence
    // of an odd number
    bool pred( int a, int b)
    {
        if (a % b != 0) {
            return 1;
        } else {
            return 0;
        }
    }
      
    // Defining the Predicate Function to find the last occurrence
    // of an even number
    bool pred1( int a, int b)
    {
        if (a % b == 0) {
            return 1;
        } else {
            return 0;
        }
    }
      
    int main()
    {
      
        // Defining a vector
        vector<int>v1 = {1, 3, 4, 5, 6, 7, 8, 10};
          
        // Declaring a sub-sequence
        vector<int>v2 = {2};
           
        // Using std::find_end to find the last occurrence of an
        // odd number
        vector<int>::iterator ip;
        ip = std::find_end(v1.begin(), v1.end(), v2.begin(),
                           v2.end(), pred);
       
        // Displaying the index where the last odd number occurred
        cout << "Last odd no. occurs at " <<  (ip - v1.begin());
      
      
        // Using std::find_end to find the last occurrence of an 
        // even number
        ip = std::find_end(v1.begin(), v1.end(), v2.begin(),
                           v2.end(), pred1);
       
        // Displaying the index where the last even number occurred
        cout << "\nLast even no. occurs at " <<  (ip - v1.begin());
      
        return 0;
    }

    chevron_right

    
    

    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:

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.



My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.