C++ std::search() Function

Definition
In C++, std::search() looks for a smaller sequence (the pattern) inside a larger range. It returns an iterator to where the first match starts.
It works on any container, so it can find a run of numbers in a vector or a word in a string.
Syntax
Add #include <algorithm>, then call std::search(). Start with these two lines.
std::search(a.begin(), a.end(), p.begin(), p.end());
std::search(a.begin(), a.end(), p.begin(), p.end(), cmp);
| Call | What it does |
|---|
std::search(a.begin(), a.end(), p.begin(), p.end()); | Find the first place where pattern p appears in a. |
std::search(a.begin(), a.end(), p.begin(), p.end(), cmp); | Compare elements with your own rule, for example ignore case. |
Library declarations
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 search(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 find. |
| pred | Optional. A function that decides whether two elements are equal. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points at the start of the first match. |
last | Returned when the pattern is not found. |
first | Returned when the pattern is empty. |
C++ Examples
Example 1 — Run of numbers
Find the numbers 4 5 6 in a bigger vector.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 4, 5, 6, 7};
std::vector<int> pattern = {4, 5, 6};
auto it = std::search(v.begin(), v.end(), pattern.begin(), pattern.end());
if (it != v.end()) {
std::cout << "Pattern starts at index " << (it - v.begin()) << '\n';
}
return 0;
}
Pattern starts at index 2
How It Works
- The pattern is a second vector, so it can hold any sequence of values.
search() slides the pattern along the data until all three values line up.- They line up at index 2, and that iterator is returned.
Example 2 — Word in a sentence
Find a word inside a sentence using iterators.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat";
std::string word = "cat";
auto it = std::search(text.begin(), text.end(), word.begin(), word.end());
if (it != text.end()) {
std::cout << "Found \"" << word << "\" at " << (it - text.begin()) << '\n';
}
return 0;
}
How It Works
text and word are both ranges of characters.- The first three characters
the do not match cat, and neither does the space. - The match begins after "the ", at index 4.
Example 3 — Ignore case
Pass a comparison function to ignore upper and lower case.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string text = "Hello World";
std::string word = "WORLD";
auto it = std::search(text.begin(), text.end(), word.begin(), word.end(),
[](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
});
std::cout << "Found at index " << (it - text.begin()) << '\n';
return 0;
}
How It Works
- The extra lambda decides when two characters count as equal.
std::tolower() turns both letters to lower case before the check.World matches WORLD starting at index 6.
Frequently Asked Questions
In C++, std::search() finds the first 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 first match, or the last iterator when the pattern is not found.
find() looks for one element. search() looks for a whole sequence of elements in order.
Yes. Pass a fourth argument, a function that compares two characters after changing them to lower case.
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.
16 people found this page helpful