C++ std::for_each_n() Function

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.
std::for_each_n(v.begin(), 3, f);
auto next = std::for_each_n(v.begin(), 2, f);
| Call | What 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
template<class InputIt, class Size, class UnaryFunc>
InputIt for_each_n(InputIt first, Size n, UnaryFunc f);
Parameters
| Parameter | Description |
|---|
| first | Required. An iterator to the first element to visit. |
| n | Required. How many elements to visit. It must not be negative. |
| f | Required. A function that takes one element. Use a reference to change it. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last visited element, which is first + n. |
C++ Examples
Example 1 — Double the first three
Change only the first three elements.
#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
v.begin(), 3 means "start at the front and visit three elements".- The reference parameter lets the lambda change each of them.
- 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.
#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
- The count 2 limits the walk to the first two strings.
- The lambda takes a
const std::string&, so there is no copy. - Chen is not printed because it is the third element.
Example 3 — Use the returned iterator
Read the element right after the visited part.
#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
- The call visits the first two values, 10 and 20.
- It returns
v.begin() + 2, the position just after the visited part. - 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.
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.
6 people found this page helpful