C++ std::search_n() Function

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

Definition

In C++, std::search_n() looks for the first place where a value repeats n times in a row. It returns an iterator to the start of that run.

It is useful for questions like "are there three free seats side by side?" or "is there a streak of zeros?".

Syntax

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

C++
std::search_n(v.begin(), v.end(), 3, 0);
std::search_n(v.begin(), v.end(), 2, 10, pred);
CallWhat it does
std::search_n(v.begin(), v.end(), 3, 0);Find three zeros in a row.
std::search_n(v.begin(), v.end(), 2, 10, pred);Use pred(element, 10) for each element of the run.
Library declarations
C++
template<class ForwardIt, class Size, class T>
ForwardIt search_n(ForwardIt first, ForwardIt last,
                   Size count, const T& value);

Parameters

ParameterDescription
first, lastRequired. The range to search.
countRequired. How many matching elements must sit next to each other.
valueRequired. The value each element must match.
predOptional. A function that compares an element with value.

Return Value

Return ValueDescription
IteratorPoints at the first element of the run.
lastReturned when no run of that length exists.

C++ Examples

Example 1 — Run of zeros

Find three zeros in a row.

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

int main() {
    std::vector<int> v = {1, 0, 0, 0, 2, 0, 0};
    auto it = std::search_n(v.begin(), v.end(), 3, 0);

    if (it != v.end()) {
        std::cout << "Three zeros start at index " << (it - v.begin()) << '\n';
    }
    return 0;
}

How It Works

  1. The arguments 3, 0 mean "three copies of the value 0".
  2. The first 1 is skipped. A run of three zeros begins right after it.
  3. The returned iterator points at the first zero of the run, index 1.

Example 2 — Run too long

Ask for a longer run than the data holds.

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

int main() {
    std::vector<int> v = {1, 0, 0, 0, 2, 0, 0};
    auto it = std::search_n(v.begin(), v.end(), 4, 0);

    if (it == v.end()) {
        std::cout << "No run of 4 zeros\n";
    }
    return 0;
}

How It Works

  1. The longest run of zeros in this data is only three.
  2. The second group of zeros has just two.
  3. No run of four exists, so the function returns v.end().

Example 3 — Custom rule

Find two values in a row that are greater than 10.

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

int main() {
    std::vector<int> v = {5, 12, 3, 14, 20, 2};
    auto it = std::search_n(v.begin(), v.end(), 2, 10,
                            [](int element, int limit) { return element > limit; });

    if (it != v.end()) {
        std::cout << "Two values above 10 start at index " << (it - v.begin()) << '\n';
    }
    return 0;
}

How It Works

  1. The predicate gets each element and the number 10 as limit.
  2. 12 passes, but the next value 3 breaks the run.
  3. 14 and 20 both pass, so the run starts at index 3.

Frequently Asked Questions

In C++, std::search_n() finds the first run of n equal elements in a row. 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 run of count matching elements, or the last iterator when there is no such run.
Yes. search_n() only finds a run of n consecutive elements.
It returns the first iterator, because a run of zero elements always matches at the start.

Next: set_difference()

Builds the difference of two sorted ranges: elements in the first but not the second.

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

18 people found this page helpful