std::ranges::transform, std::ranges::unary_transform_result, std::ranges::binary_transform_result
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
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< ranges::input_range R1, ranges::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 起) |
將給定函式應用於一個範圍,並將結果儲存在另一個以 result 開頭的範圍中。
[
first1,
last1)
定義的範圍(在透過投影 proj 投影之後)。[
first1,
last1)
定義,另一個由 [
first2,
last2)
定義(在分別透過投影 proj1 和 proj2 投影之後)。本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first1, last1 | - | 定義要轉換的第一個範圍元素的迭代器-哨兵對 |
r, r1 | - | 要轉換的第一個元素範圍 |
first2, last2 | - | 定義要轉換的第二個範圍元素的迭代器-哨兵對 |
r2 | - | 要轉換的第二個元素範圍 |
result | - | 目標範圍的起始,可以等於 first1 或 first2 |
op, binary_op | - | 應用於投影元素的操作 |
proj1 | - | 應用於第一個範圍元素的投影。 |
proj2 | - | 應用於第二個範圍元素的投影。 |
[編輯] 返回值
unary_transform_result
,包含一個等於 last 的輸入迭代器,以及一個指向最後一個被轉換元素之後元素的輸出迭代器。binary_transform_result
,包含指向來自範圍 [
first1,
last1)
和 [
first2,
last2)
的最後轉換元素的輸入迭代器(分別命名為 in1
和 in2
),以及一個指向最後一個被轉換元素之後元素的輸出迭代器(命名為 out
)。[編輯] 複雜度
[編輯] 可能的實現
struct transform_fn { // First version 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 {std::move(first1), std::move(result)}; } // Second version 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), std::move(result), std::move(op), std::move(proj)); } // Third version 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 {std::move(first1), std::move(first2), std::move(result)}; } // Fourth version 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::indirect_result_t<F&, 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), std::move(result), std::move(binary_op), std::move(proj1), std::move(proj2)); } }; inline constexpr transform_fn transform; |
[編輯] 注意
ranges::transform
不保證 op 或 binary_op 的按序應用。要按序將函式應用於序列或應用修改序列元素的函式,請使用 ranges::for_each。
[編輯] 示例
以下程式碼使用 ranges::transform
將字串原地轉換為大寫,使用 std::toupper 函式,然後將每個 char 轉換為其序數值。然後使用帶有投影的 ranges::transform
將 std::vector<Foo> 的元素轉換為字元以填充 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'; }
輸出
HELLO: 72 69 76 76 79 144 138 152 152 158 HELLO
[編輯] 參閱
(C++20) |
對範圍中的元素應用一元函式物件 (演算法函式物件) |
一個將轉換函式應用於每個元素的序列 view (類模板) (範圍介面卡物件) | |
對一個範圍的元素應用函式,並將結果儲存在目標範圍中 (函式模板) |