C++ std::for_each_n() Function

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

Definition

In C++17, std::for_each_n() is like std::for_each(), but you give it a starting iterator and a count instead of two iterators.

It visits exactly n elements and returns an iterator just after the last one it visited. That makes it easy to continue from that point.

Syntax

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

C++
std::for_each_n(v.begin(), 3, f);
auto next = std::for_each_n(v.begin(), 2, f);
CallWhat it does
std::for_each_n(v.begin(), 3, f);Call f on the first three elements.
auto next = std::for_each_n(v.begin(), 2, f);Keep the returned iterator to continue from there.

Compile with -std=c++17 or newer.

Library declarations
C++
template<class InputIt, class Size, class UnaryFunc>
InputIt for_each_n(InputIt first, Size n, UnaryFunc f);

Parameters

ParameterDescription
firstRequired. An iterator to the first element to visit.
nRequired. How many elements to visit. It must not be negative.
fRequired. A function that takes one element. Use a reference to change it.

Return Value

Return ValueDescription
IteratorPoints just after the last visited element, which is first + n.

C++ Examples

Example 1 — Double the first three

Change only the first three elements.

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

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

    std::for_each_n(v.begin(), 3, [](int& n) { n *= 2; });

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. v.begin(), 3 means "start at the front and visit three elements".
  2. The reference parameter lets the lambda change each of them.
  3. The last two elements are never visited, so 4 and 5 stay the same.

Example 2 — Print the first two names

Visit the start of a vector of strings.

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

int main() {
    std::vector<std::string> names = {"Asha", "Ben", "Chen"};

    std::for_each_n(names.begin(), 2, [](const std::string& s) {
        std::cout << s << '\n';
    });
    return 0;
}

How It Works

  1. The count 2 limits the walk to the first two strings.
  2. The lambda takes a const std::string&, so there is no copy.
  3. Chen is not printed because it is the third element.

Example 3 — Use the returned iterator

Read the element right after the visited part.

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

int main() {
    std::vector<int> v = {10, 20, 30};

    auto it = std::for_each_n(v.begin(), 2, [](int n) {
        std::cout << n << ' ';
    });
    std::cout << "\nNext: " << *it << '\n';
    return 0;
}

How It Works

  1. The call visits the first two values, 10 and 20.
  2. It returns v.begin() + 2, the position just after the visited part.
  3. Dereferencing that iterator reads 30.

Frequently Asked Questions

In C++, std::for_each_n() calls a function on the first n elements of a range. Include <algorithm> to use it.
Include <algorithm>. It needs C++17 or newer, so compile with -std=c++17 or later.
It returns an iterator to the element after the last one that was visited.
for_each() uses two iterators. for_each_n() uses one iterator and a count, so you do not need an end iterator.
C++17. Compile with -std=c++17 or newer.

Next: generate()

Fills a range with values made by calling a function.

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

6 people found this page helpful