
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.
std::transform(v.begin(), v.end(), out.begin(), f);
std::transform(a.begin(), a.end(), b.begin(), out.begin(), f);
| Call | What 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
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
| Parameter | Description |
|---|
| first, last | Required. The source range. |
| first2 | Only for the two-range version. Where the second source range starts. |
| d_first | Required. Where the results are written. It can be the same as first. |
| op | Required. A function that takes one element (or two) and returns the new value. |
Return Value
| Return Value | Description |
|---|
| Iterator | Points just after the last element written. |
C++ Examples
Example 1 — Double each number
Write twice each value into a second vector.
#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
- The lambda takes one number and returns its double.
transform() calls it for 1, 2 and 3 and writes each result into out.- The source vector
v is not changed.
Example 2 — Make text upper case
Change a string in place.
#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
- The destination is
s.begin(), the same as the source, so the string changes in place. std::toupper() converts each letter.- The
unsigned char parameter avoids trouble with special characters.
Example 3 — Add two vectors
Combine two ranges with std::plus.
#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
- This version reads one element from
a and one from b at the same time. std::plus<int>() adds the two numbers.- 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
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.
10 people found this page helpful