How to Use std::transform in C++: Syntax and Examples
In C++,
std::transform applies a function to elements in a range and stores the results in another range. It requires input iterators for the source, an output iterator for the destination, and a unary or binary operation to apply.Syntax
The std::transform function has two main forms:
- Unary operation: applies a function to each element in one input range.
- Binary operation: applies a function to pairs of elements from two input ranges.
Parameters explained:
first: start iterator of input rangelast: end iterator of input ranged_first: start iterator of output rangeunary_op: function taking one argument (for unary)first2: start iterator of second input range (for binary)binary_op: function taking two arguments (for binary)
cpp
template<class InputIt, class OutputIt, class UnaryOperation> OutputIt transform(InputIt first, InputIt last, OutputIt d_first, UnaryOperation unary_op); template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation> OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op);
Example
This example shows how to use std::transform to square numbers in a vector and then add corresponding elements from two vectors.
cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; std::vector<int> squares(numbers.size()); // Unary transform: square each number std::transform(numbers.begin(), numbers.end(), squares.begin(), [](int x) { return x * x; }); std::cout << "Squares: "; for (int n : squares) { std::cout << n << ' '; } std::cout << '\n'; std::vector<int> other = {10, 20, 30, 40, 50}; std::vector<int> sums(numbers.size()); // Binary transform: add elements from numbers and other std::transform(numbers.begin(), numbers.end(), other.begin(), sums.begin(), [](int a, int b) { return a + b; }); std::cout << "Sums: "; for (int n : sums) { std::cout << n << ' '; } std::cout << '\n'; return 0; }
Output
Squares: 1 4 9 16 25
Sums: 11 22 33 44 55
Common Pitfalls
Common mistakes when using std::transform include:
- Not ensuring the output range has enough space, which can cause undefined behavior.
- Mixing input and output iterators incorrectly.
- Using a binary transform but providing only one input range.
- Forgetting to include the
<algorithm>header.
Always prepare the output container with the correct size before calling std::transform.
cpp
#include <vector> #include <algorithm> // Wrong: output vector not resized std::vector<int> input = {1, 2, 3}; std::vector<int> output; // empty vector // This will cause undefined behavior because output has no space // std::transform(input.begin(), input.end(), output.begin(), [](int x) { return x * 2; }); // Correct way: std::vector<int> output_correct(input.size()); std::transform(input.begin(), input.end(), output_correct.begin(), [](int x) { return x * 2; });
Quick Reference
std::transform Cheat Sheet:
| Parameter | Description |
|---|---|
first, last | Input range to transform |
d_first | Start of output range |
unary_op | Function applied to each element (one argument) |
first2 | Second input range (for binary transform) |
binary_op | Function applied to pairs of elements (two arguments) |
| Parameter | Description |
|---|---|
| first, last | Input range to transform |
| d_first | Start of output range |
| unary_op | Function applied to each element (one argument) |
| first2 | Second input range (for binary transform) |
| binary_op | Function applied to pairs of elements (two arguments) |
Key Takeaways
std::transform applies a function to elements in input ranges and writes results to an output range.
Always ensure the output container has enough space before calling std::transform.
Use unary transform for one input range and binary transform for two input ranges.
Include and use iterators to specify ranges.
Lambda functions are a convenient way to define operations inline.