Home C++ Algorithms Functions find_end() C++ std::find_end() Function 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.
std::find_end(a.begin(), a.end(), p.begin(), p.end());
std::find_end(a.begin(), a.end(), p.begin(), p.end(), cmp); Call What 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 template<class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last); Parameters Parameter Description first, last Required. The range to search in. s_first, s_last Required. The pattern to look for. pred Optional. A function that decides if two elements are equal.
Return Value Return Value Description Iterator Points at the first element of the last match. lastReturned when the pattern is not found, or when the pattern is empty.
C++ Examples 1. Last match in a vector 2. Pattern not found 3. Last word in text Example 1 — Last match in a vector The pattern 1 2 3 appears twice. Find the second one.
#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;
} Last match starts at index 3 How It Works The pattern 1 2 3 appears at index 0 and again at index 3. find_end() keeps the later one.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.
#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 The pattern 3 2 would need a 3 followed straight away by a 2. In this vector every 3 is followed by 1 or 4, so there is no match. 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.
#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;
} Last "ab" starts at index 6 How It Works The text has ab at indexes 0, 3 and 6. find_end() returns the iterator for the final one.Each string is a range of characters, so the same call works on text. Frequently Asked Questions What does std::find_end() do? In C++, std::find_end() finds the last occurrence of a sequence inside a range. Include <algorithm> to use it.
Which header and C++ version do I need for std::find_end()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::find_end() return? It returns an iterator to the start of the last match, or the last iterator when there is no match.
What is the difference between find_end() and search()? search() returns the first match of a sequence. find_end() returns the last match.
What happens if the pattern is empty? find_end() returns the last iterator, because an empty pattern is treated as not found.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion