C++ std::for_each() Function

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

Definition

In C++, std::for_each() runs a function once for every element in a range, from first to last.

The function can print each value, change it (if it takes a reference), or add it to a running total. It is an algorithm version of a simple loop.

Syntax

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

C++
std::for_each(v.begin(), v.end(), f);
std::for_each(v.begin(), v.end(), [](int& n) { n += 10; });
CallWhat it does
std::for_each(v.begin(), v.end(), f);Call f on every element.
std::for_each(v.begin(), v.end(), [](int& n) { n += 10; });Take a reference to change the element in place.
Library declarations
C++
template<class InputIt, class UnaryFunc>
UnaryFunc for_each(InputIt first, InputIt last, UnaryFunc f);

Parameters

ParameterDescription
first, lastRequired. The range to visit.
fRequired. A function or lambda that takes one element. Use int& if you want to change it.

Return Value

Return ValueDescription
FunctionThe function object f is returned (moved). Most programs ignore it.

C++ Examples

Example 1 — Print each value doubled

Visit each number and print twice its value.

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

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

    std::for_each(v.begin(), v.end(), [](int n) {
        std::cout << n * 2 << ' ';
    });
    std::cout << '\n';
    return 0;
}

How It Works

  1. The lambda receives each number by value, so the vector is not changed.
  2. It prints twice the value followed by a space.
  3. for_each() calls it for 1, 2 and 3, giving 2 4 6.

Example 2 — Change values in place

Add 10 to every element using a reference.

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

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

    std::for_each(v.begin(), v.end(), [](int& n) { n += 10; });

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

How It Works

  1. int& n is a reference, so n += 10 changes the real element in the vector.
  2. Each of the three elements gets 10 added.
  3. The loop afterwards prints the updated vector.

Example 3 — Build a total

Use a captured variable to add up all values.

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

int main() {
    std::vector<int> v = {4, 5, 6};
    int sum = 0;

    std::for_each(v.begin(), v.end(), [&sum](int n) { sum += n; });

    std::cout << "Sum: " << sum << '\n';
    return 0;
}

How It Works

  1. [&sum] lets the lambda change the outside variable sum.
  2. The lambda adds 4, then 5, then 6.
  3. After the call, sum holds 15.

Frequently Asked Questions

In C++, std::for_each() calls a function on every element in 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 the function object that was passed in, usually ignored.
They do the same job. A range-based for loop is often simpler to read. for_each() is useful when you already have a function to reuse or only want part of a range.
No. It always visits every element. Use std::find_if() or a normal loop with break when you need to stop.

Next: for_each_n()

Calls a function on the first n elements of a range.

for_each_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.

23 people found this page helpful