C++ std::transform() Function

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

Definition

In C++, std::transform() takes each element of a range, applies a function to it, and writes the result to a destination.

The destination can be a new container, or the same one if you want to change the elements in place. There is also a version that combines two ranges.

Syntax

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

C++
std::transform(v.begin(), v.end(), out.begin(), f);
std::transform(a.begin(), a.end(), b.begin(), out.begin(), f);
CallWhat it does
std::transform(v.begin(), v.end(), out.begin(), f);Apply f to each element and write to out.
std::transform(a.begin(), a.end(), b.begin(), out.begin(), f);Combine two ranges element by element.
Library declarations
C++
template<class InputIt, class OutputIt, class UnaryOp>
OutputIt transform(InputIt first, InputIt last,
                   OutputIt d_first, UnaryOp op);

template<class InputIt1, class InputIt2, class OutputIt, class BinaryOp>
OutputIt transform(InputIt1 first1, InputIt1 last1,
                   InputIt2 first2, OutputIt d_first, BinaryOp op);

Parameters

ParameterDescription
first, lastRequired. The source range.
first2Only for the two-range version. Where the second source range starts.
d_firstRequired. Where the results are written. It can be the same as first.
opRequired. A function that takes one element (or two) and returns the new value.

Return Value

Return ValueDescription
IteratorPoints just after the last element written.

C++ Examples

Example 1 — Double each number

Write twice each value into a second vector.

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

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

    std::transform(v.begin(), v.end(), out.begin(), [](int n) { return n * 2; });

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

How It Works

  1. The lambda takes one number and returns its double.
  2. transform() calls it for 1, 2 and 3 and writes each result into out.
  3. The source vector v is not changed.

Example 2 — Make text upper case

Change a string in place.

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

int main() {
    std::string s = "hello";

    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
        return static_cast<char>(std::toupper(c));
    });

    std::cout << s << '\n';
    return 0;
}

How It Works

  1. The destination is s.begin(), the same as the source, so the string changes in place.
  2. std::toupper() converts each letter.
  3. The unsigned char parameter avoids trouble with special characters.

Example 3 — Add two vectors

Combine two ranges with std::plus.

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

int main() {
    std::vector<int> a = {1, 2, 3};
    std::vector<int> b = {10, 20, 30};
    std::vector<int> sum(3);

    std::transform(a.begin(), a.end(), b.begin(), sum.begin(), std::plus<int>());

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

How It Works

  1. This version reads one element from a and one from b at the same time.
  2. std::plus<int>() adds the two numbers.
  3. The three sums are written into sum.

Frequently Asked Questions

In C++, std::transform() applies a function to each element and stores the results. 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 element after the last one that was written.
Yes. Pass the same begin iterator as both the source start and the destination start.
transform() stores the returned value of a function. for_each() just calls a function and ignores any return value.

Next: unique()

Removes consecutive duplicate elements and returns the new end.

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

10 people found this page helpful