On this page
std::ranges::transform, std::ranges::unary_transform_result, std::ranges::binary_transform_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
|
(3) | (since C++20) |
|
(4) | (since C++20) |
Helper types | ||
|
(5) | (since C++20) |
|
(6) | (since C++20) |
Applies the given function to a range and stores the result in another range, beginning at result
.
op
is applied to the range defined by [
first1
,
last1
)
(after projecting with the projection proj
).
r
as the source range, as if using ranges::begin(r)
as first
and ranges::end(r)
as last
.
binary_op
is applied to pairs of elements from two ranges: one defined by [
first1
,
last1
)
and the other defined by [
first2
,
last2
)
(after respectively projecting with the projections proj1
and proj2
).
r1
as the first source range, as if using ranges::begin(r1)
as first1
and ranges::end(r1)
as last1
, and similarly for r2
.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
first1, last1 | - | the first range of elements to transform |
r, r1 | - | the first range of elements to transform |
first2, last2 | - | the second range of elements to transform |
r2 | - | the second range of elements to transform |
result | - | the beginning of the destination range, may be equal to first1 or first2 |
op, binary_op | - | operation to apply to the projected element(s) |
proj1 | - | projection to apply to the elements in the first range |
proj2 | - | projection to apply to the elements in the second range |
Return value
unary_transform_result
contains an input iterator equal to last
and an output iterator to the element past the last element transformed.
binary_transform_result
contains input iterators to last transformed elements from ranges [
first1
,
last1
)
and [
first2
,
last2
)
as in1
and in2
respectively, and the output iterator to the element past the last element transformed as out
.
Complexity
ranges::min(ranges::distance(first1, last1), ranges::distance(first2, last2))
applications of binary_op
and projections.
Possible implementation
|
Notes
ranges::transform
does not guarantee in-order application of op
or binary_op
. To apply a function to a sequence in-order or to apply a function that modifies the elements of a sequence, use ranges::for_each
.
Example
The following code uses ranges::transform
to convert a string in place to uppercase using the std::toupper
function and then transforms each char
to its ordinal value. Then ranges::transform
with a projection is used to transform elements of std::vector<Foo>
into chars to fill a std::string
.
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string s{"hello"};
auto op = [](unsigned char c) -> unsigned char { return std::toupper(c); };
namespace ranges = std::ranges;
// uppercase the string in-place
ranges::transform(s.begin(), s.end(), s.begin(), op );
std::vector<std::size_t> ordinals;
// convert each char to size_t
ranges::transform(s, std::back_inserter(ordinals),
[](unsigned char c) -> std::size_t { return c; });
std::cout << s << ':';
for (auto ord : ordinals)
std::cout << ' ' << ord;
// double each ordinal
ranges::transform(ordinals, ordinals, ordinals.begin(), std::plus {});
std::cout << '\n';
for (auto ord : ordinals)
std::cout << ord << ' ';
std::cout << '\n';
struct Foo
{
char bar;
};
const std::vector<Foo> f = {{'h'},{'e'},{'l'},{'l'},{'o'}};
std::string result;
// project, then uppercase
ranges::transform(f, std::back_inserter(result), op, &Foo::bar);
std::cout << result << '\n';
}
Output:
HELLO: 72 69 76 76 79
144 138 152 152 158
HELLO
See also
(C++20)
|
applies a function to a range of elements (niebloid) |
(C++20)
|
a view of a sequence that applies a transformation function to each element(class template) (range adaptor object) |
applies a function to a range of elements, storing results in a destination range (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/transform