C++ std::find_end() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::find_end() searches a range for a smaller sequence and returns an iterator to where the last match begins.

Despite the name, it does not look at the end of the range only. It finds the final place where the whole pattern appears. To find the first place, use std::search().

Syntax

Add #include <algorithm>, then call std::find_end(). Start with these two lines.

C++
std::find_end(a.begin(), a.end(), p.begin(), p.end());
std::find_end(a.begin(), a.end(), p.begin(), p.end(), cmp);
CallWhat it does
std::find_end(a.begin(), a.end(), p.begin(), p.end());Find the last place where pattern p appears in a.
std::find_end(a.begin(), a.end(), p.begin(), p.end(), cmp);Compare elements with your own function.
Library declarations
C++
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last);

Parameters

ParameterDescription
first, lastRequired. The range to search in.
s_first, s_lastRequired. The pattern to look for.
predOptional. A function that decides if two elements are equal.

Return Value

Return ValueDescription
IteratorPoints at the first element of the last match.
lastReturned when the pattern is not found, or when the pattern is empty.

C++ Examples

Example 1 — Last match in a vector

The pattern 1 2 3 appears twice. Find the second one.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 1, 2, 3, 4};
    std::vector<int> pattern = {1, 2, 3};

    auto it = std::find_end(v.begin(), v.end(), pattern.begin(), pattern.end());
    std::cout << "Last match starts at index " << (it - v.begin()) << '\n';
    return 0;
}

How It Works

  1. The pattern 1 2 3 appears at index 0 and again at index 3.
  2. find_end() keeps the later one.
  3. Subtracting v.begin() from the iterator gives the index 3.

Example 2 — Pattern not found

Search for a pattern that is not in the data.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 1, 2, 3, 4};
    std::vector<int> pattern = {3, 2};

    auto it = std::find_end(v.begin(), v.end(), pattern.begin(), pattern.end());
    if (it == v.end()) {
        std::cout << "Pattern not found\n";
    }
    return 0;
}

How It Works

  1. The pattern 3 2 would need a 3 followed straight away by a 2.
  2. In this vector every 3 is followed by 1 or 4, so there is no match.
  3. A failed search returns the last iterator, which is v.end().

Example 3 — Last word in text

Find the last time a short word appears in a string.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string text = "abcabcab";
    std::string word = "ab";

    auto it = std::find_end(text.begin(), text.end(), word.begin(), word.end());
    std::cout << "Last \"ab\" starts at index " << (it - text.begin()) << '\n';
    return 0;
}

How It Works

  1. The text has ab at indexes 0, 3 and 6.
  2. find_end() returns the iterator for the final one.
  3. Each string is a range of characters, so the same call works on text.

Frequently Asked Questions

In C++, std::find_end() finds the last occurrence of a sequence inside a range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the start of the last match, or the last iterator when there is no match.
search() returns the first match of a sequence. find_end() returns the last match.
find_end() returns the last iterator, because an empty pattern is treated as not found.

Next: find_first_of()

Finds the first element that matches any value from a second range.

find_first_of() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

21 people found this page helpful