C++ std::search() Function

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

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.

C++
std::search(a.begin(), a.end(), p.begin(), p.end());
std::search(a.begin(), a.end(), p.begin(), p.end(), cmp);
CallWhat 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
C++
template<class ForwardIt1, class ForwardIt2>
ForwardIt1 search(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 find.
predOptional. A function that decides whether two elements are equal.

Return Value

Return ValueDescription
IteratorPoints at the start of the first match.
lastReturned when the pattern is not found.
firstReturned when the pattern is empty.

C++ Examples

Example 1 — Run of numbers

Find the numbers 4 5 6 in a bigger vector.

C++
#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;
}

How It Works

  1. The pattern is a second vector, so it can hold any sequence of values.
  2. search() slides the pattern along the data until all three values line up.
  3. 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.

C++
#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

  1. text and word are both ranges of characters.
  2. The first three characters the do not match cat, and neither does the space.
  3. The match begins after "the ", at index 4.

Example 3 — Ignore case

Pass a comparison function to ignore upper and lower case.

C++
#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

  1. The extra lambda decides when two characters count as equal.
  2. std::tolower() turns both letters to lower case before the check.
  3. 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.

Next: search_n()

Finds the first run of n equal elements in a row.

search_n() 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.

16 people found this page helpful