std::transform
定義於標頭檔案 <algorithm> |
||
template< class InputIt, class OutputIt, class UnaryOp > OutputIt transform( InputIt first1, InputIt last1, |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOp > |
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOp > |
(3) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (C++17 起) |
std::transform
將給定函式應用於給定輸入範圍的元素,並將結果儲存在從 d_first 開始的輸出範圍中。
[
first1,
last1)
的元素。-
[
first1,
last1]
. - 從 d_first 開始的 std::distance(first1, last1) + 1 個元素的範圍。
[
first1,
last1)
和從 first2 開始的 std::distance(first1, last1) 個元素的另一個範圍。-
[
first1,
last1]
. - 從 first2 開始的 std::distance(first1, last1) + 1 個元素的範圍。
- 從 d_first 開始的 std::distance(first1, last1) + 1 個元素的範圍。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 為 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 為 true。 |
(C++20 起) |
目錄 |
[編輯] 引數
first1, last1 | - | 定義要轉換的元素源 範圍 的迭代器對 |
first2 | - | 要轉換的第二個元素範圍的開頭,僅限 (3,4) |
d_first | - | 目標範圍的開頭,可能等於 first1 或 first2 |
policy | - | 要使用的 執行策略 |
unary_op | - | 將應用的一元操作函式物件。 函式的簽名應等效於以下內容 Ret fun(const Type &a); 簽名不需要有 const &。 |
binary_op | - | 將應用的二元操作函式物件。 函式的簽名應等效於以下內容 Ret fun(const Type1 &a, const Type2 &b); 簽名不需要有 const &。 |
型別要求 | ||
-InputIt, InputIt1, InputIt2 必須滿足 LegacyInputIterator 的要求。 | ||
-OutputIt 必須滿足 LegacyOutputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2, ForwardIt3 必須滿足 LegacyForwardIterator 的要求。 |
[編輯] 返回值
指向最後一個轉換元素之後的元素的輸出迭代器。
[編輯] 複雜度
給定 N 作為 std::distance(first1, last1)
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 如果作為演算法一部分呼叫的函式執行時丟擲異常,並且
ExecutionPolicy
是 標準策略 之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 可能的實現
transform (1) |
---|
template<class InputIt, class OutputIt, class UnaryOp> constexpr //< since C++20 OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOp unary_op) { for (; first1 != last1; ++d_first, ++first1) *d_first = unary_op(*first1); return d_first; } |
transform (3) |
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOp> constexpr //< since C++20 OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOp binary_op) { for (; first1 != last1; ++d_first, ++first1, ++first2) *d_first = binary_op(*first1, *first2); return d_first; } |
[編輯] 注意
std::transform
不保證按順序應用 unary_op 或 binary_op。要按順序對序列應用函式或應用修改序列元素的函式,請使用 std::for_each。
[編輯] 示例
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <string> #include <utility> #include <vector> void print_ordinals(const std::vector<unsigned>& ordinals) { std::cout << "ordinals: "; for (unsigned ord : ordinals) std::cout << std::setw(3) << ord << ' '; std::cout << '\n'; } char to_uppercase(unsigned char c) { return std::toupper(c); } void to_uppercase_inplace(char& c) { c = to_uppercase(c); } void unary_transform_example(std::string& hello, std::string world) { // Transform string to uppercase in-place std::transform(hello.cbegin(), hello.cend(), hello.begin(), to_uppercase); std::cout << "hello = " << std::quoted(hello) << '\n'; // for_each version (see Notes above) std::for_each(world.begin(), world.end(), to_uppercase_inplace); std::cout << "world = " << std::quoted(world) << '\n'; } void binary_transform_example(std::vector<unsigned> ordinals) { // Transform numbers to doubled values print_ordinals(ordinals); std::transform(ordinals.cbegin(), ordinals.cend(), ordinals.cbegin(), ordinals.begin(), std::plus<>{}); print_ordinals(ordinals); } int main() { std::string hello("hello"); unary_transform_example(hello, "world"); std::vector<unsigned> ordinals; std::copy(hello.cbegin(), hello.cend(), std::back_inserter(ordinals)); binary_transform_example(std::move(ordinals)); }
輸出
hello = "HELLO" world = "WORLD" ordinals: 72 69 76 76 79 ordinals: 144 138 152 152 158
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 242 | C++98 | unary_op 和 binary_op 不能有副作用 | 它們不能修改所涉及的範圍 |
[編輯] 另請參閱
對範圍中的元素應用一元函式物件 (函式模板) | |
(C++20) |
對一個範圍的元素應用函式 (演算法函式物件) |