std::ranges::transform, std::ranges::unary_transform_result, std::ranges::binary_transfor_result
定义于头文件 <algorithm>
|
||
调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity > |
(1) | (C++20 起) |
template< ranges::input_range R, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity > |
(2) | (C++20 起) |
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(3) | (C++20 起) |
template< std::input_range R1, std::input_range R2, |
(4) | (C++20 起) |
辅助类型 |
||
template < class I, class O > using unary_transform_result = ranges::in_out_result<I, O>; |
(5) | (C++20 起) |
template < class I1, class I2, class O > using binary_transform_result = ranges::in_in_out_result<I1, I2, O>; |
(6) | (C++20 起) |
ranges::transform
应用给定的函数到范围并存储结果于始于 result
的另一范围。
op
到 [first1, last1)
所定义的范围(在以投影 proj
投影后)。r
为源范围,如同以 ranges::begin(r) 为 first
并以 ranges::end(r) 为 last
。binary_op
到来自二个范围的元素:一个由 [first1, last1)
定义而另一个由 [first2, last2)
定义(在分别以投影 proj1
与 proj2
投影后)。r1
为第一源范围,如同以 ranges::begin(r1) 为 first1
并以 ranges::end(r1) 为 last1
,而对 r2
也类似。此页面上描述的仿函数实体是 niebloid ,即:
实际上,它们能以函数对象,或以某些特殊编译器扩展实现。
参数
first1, last1 | - | 要变换的元素范围 |
r, r1 | - | 要变换的元素范围
{{par | first2, last2 |要变换的第二元素范围} |
r2 | - | 要变换的第二元素范围 |
result | - | 目标范围的起始,可以等于 first1 或 first2
|
op, binary_op | - | 应用到投影后元素的操作 |
proj1 | - | 应用到第一范围中的元素的投影 |
proj2 | - | 应用到第二范围中的元素的投影 |
返回值
unary_transform_result
,含有等于 last
的输入迭代器与指向最后变换元素后一元素的输出迭代器。binary_transform_result
,含有分别作为 in1
与 in2
的指向来自范围 [first1, last1)
与 [first2, last2)
的最后变换元素的输入迭代器,与作为 out
的指向最后变换元素后一元素的输出迭代器。复杂度
op
与 proj
。binary_op
与投影。可能的实现
struct transform_fn { template< std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity > requires std::indirectly_writable<O, std::indirect_result_t<F&, std::projected<I, Proj>>> constexpr ranges::unary_transform_result<I, O> operator()( I first1, S last1, O result, F op, Proj proj = {} ) const { for (; first1 != last1; ++first1, (void)++result) { *result = std::invoke(op, std::invoke(proj, *first1)); } return {first1, result}; } template< ranges::input_range R, std::weakly_incrementable O, std::copy_constructible F, class Proj = std::identity > requires std::indirectly_writable< O, std::indirect_result_t<F&, std::projected<ranges::iterator_t<R>, Proj>>> constexpr ranges::unary_transform_result<ranges::borrowed_iterator_t<R>, O> operator()( R&& r, O result, F op, Proj proj = {} ) const { return (*this)(ranges::begin(r), ranges::end(r), result, std::ref(proj)); } template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, std::weakly_incrementable O, std::copy_constructible F, class Proj1 = std::identity, class Proj2 = std::identity > requires std::indirectly_writable< O, std::indirect_result_t<F&, std::projected<I1, Proj1>, std::projected<I2, Proj2>>> constexpr ranges::binary_transform_result<I1, I2, O> operator()( I1 first1, S1 last1, I2 first2, S2 last2, O result, F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {} ) const { for (; first1 != last1 && first2 != last2; ++first1, (void)++first2, (void)++result) { *result = std::invoke(binary_op, std::invoke(proj1, *first1), std::invoke(proj2, *first2)); } return {first1, first2, result}; } template< ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O, std::copy_constructible F, class Proj1 = std::identity, class Proj2 = std::identity > requires std::indirectly_writable< O, std::projected<ranges::iterator_t<R1>, Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>> constexpr ranges::binary_transform_result<ranges::borrowed_iterator_t<R1>, ranges::borrowed_iterator_t<R2>, O> operator()( R1&& r1, R2&& r2, O result, F binary_op, Proj1 proj1 = {}, Proj2 proj2 = {} ) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), result, std::ref(binary_op), std::ref(proj1), std::ref(proj2)); } }; inline constexpr transform_fn transform; |
注解
std::ranges::transform
不保证按顺序应用 op
或 binary_op
。为按顺序应用函数导序列或应用修改元素的函数到序列,请用 std::ranges::for_each 。
示例
下列代码用 transform
以 toupper
函数原地转换为大写然后变换每个 char
为其序数值:
#include <algorithm> #include <cctype> #include <functional> #include <iostream> #include <string> #include <vector> int main() { std::string s("hello"); namespace ranges = std::ranges; ranges::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) -> unsigned char { return std::toupper(c); }); std::vector<std::size_t> ordinals; 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; } ranges::transform(ordinals, ordinals, ordinals.begin(), std::plus{}); std::cout << '\n'; for (auto ord : ordinals) { std::cout << ord << ' '; } std::cout << '\n'; }
输出:
HELLO: 72 69 76 76 79 144 138 152 152 158
参阅
(C++20) |
应用函数到范围中的元素 (niebloid) |
对序列的每个元素应用某个变换函数的 view (类模板) (范围适配器对象) | |
将一个函数应用于某一范围的各个元素,并在目标范围存储结果 (函数模板) |