std::reduce
來自 cppreference.com
定義於標頭檔案 <numeric> |
||
template< class InputIt > typename std::iterator_traits<InputIt>::value_type |
(1) | (C++17 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt > typename std::iterator_traits<ForwardIt>::value_type |
(2) | (C++17 起) |
template< class InputIt, class T > T reduce( InputIt first, InputIt last, T init ); |
(3) | (C++17 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class T > T reduce( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
template< class InputIt, class T, class BinaryOp > T reduce( InputIt first, InputIt last, T init, BinaryOp op ); |
(5) | (C++17 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class T, class BinaryOp > |
(6) | (C++17 起) |
1) 等價於 reduce(first, last, typename std::iterator_traits<InputIt>::value_type{})。
3) 等價於 reduce(first, last, init, std::plus<>())。
5) 將範圍
[
first,
last)
中的元素與初始值 init,透過 op 以未指定的方式進行置換和聚合,從而進行歸約。2,4,6) 與 (1,3,5) 相同,但根據 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議
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 起) |
給定 binary_op 作為實際的二元操作,
- 如果 binary_op 不滿足結合律或交換律(例如浮點數加法),則結果是非確定性的。
- 如果以下任一值不能轉換為 `T`,則程式是病態的:
- binary_op(init, *first)
- binary_op(*first, init)
- binary_op(init, init)
- binary_op(*first, *first)
- 如果滿足以下任何條件,則行為是未定義的:
-
T
不是 可移動構造 (MoveConstructible) 的。 - binary_op 修改了
[
first,
last)
中的任何元素。 - binary_op 使
[
first,
last]
的任何迭代器或子範圍失效。
-
目錄 |
[編輯] 引數
first, last | - | 定義要應用演算法的元素範圍的迭代器對 |
init | - | 廣義和的初始值 |
policy | - | 要使用的 執行策略 |
op | - | 二元 函式物件 (FunctionObject),將以未指定順序應用於解引用輸入迭代器的結果、其他 op 的結果和 init。 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 |
[編輯] 返回值
5,6) init 和
[
first,
last)
中元素在 op 上的廣義和。一組元素在二元操作 binary_op 上的廣義和定義如下:
- 如果組中只有一個元素,則和為該元素的值。
- 否則,按順序執行以下操作:
- 從組中取出任意兩個元素 elem1 和 elem2。
- 計算 binary_op(elem1, elem2) 並將結果放回組中。
- 重複步驟 1 和 2,直到組中只有一個元素。
[編輯] 複雜度
給定 N 為 std::distance(first, last)
5,6) O(N) 次 op 的應用。
[編輯] 異常
帶有模板引數 ExecutionPolicy
的過載按如下方式報告錯誤
- 如果作為演算法一部分呼叫的函式執行丟擲異常,並且
ExecutionPolicy
是標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為是實現定義的。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[編輯] 注意
std::reduce
的行為類似於 std::accumulate,但範圍內的元素可以以任意順序進行分組和重新排列。
[編輯] 示例
std::reduce
和 std::accumulate 的並排比較
執行此程式碼
#if PARALLEL #include <execution> #define SEQ std::execution::seq, #define PAR std::execution::par, #else #define SEQ #define PAR #endif #include <chrono> #include <iomanip> #include <iostream> #include <locale> #include <numeric> #include <utility> #include <vector> int main() { std::cout.imbue(std::locale("en_US.UTF-8")); std::cout << std::fixed << std::setprecision(1); auto eval = [](auto fun) { const auto t1 = std::chrono::high_resolution_clock::now(); const auto [name, result] = fun(); const auto t2 = std::chrono::high_resolution_clock::now(); const std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << std::setw(28) << std::left << name << "sum: " << result << '\t' << "time: " << ms.count() << " ms\n"; }; { const std::vector<double> v(100'000'007, 0.1); eval([&v]{ return std::pair{"std::accumulate (double)", std::accumulate(v.cbegin(), v.cend(), 0.0)}; }); eval([&v]{ return std::pair{"std::reduce (seq, double)", std::reduce(SEQ v.cbegin(), v.cend())}; }); eval([&v]{ return std::pair{"std::reduce (par, double)", std::reduce(PAR v.cbegin(), v.cend())}; }); } { const std::vector<long> v(100'000'007, 1); eval([&v]{ return std::pair{"std::accumulate (long)", std::accumulate(v.cbegin(), v.cend(), 0l)}; }); eval([&v]{ return std::pair{"std::reduce (seq, long)", std::reduce(SEQ v.cbegin(), v.cend())}; }); eval([&v]{ return std::pair{"std::reduce (par, long)", std::reduce(PAR v.cbegin(), v.cend())}; }); } }
可能的輸出
// POSIX: g++ -std=c++23 ./example.cpp -ltbb -O3; ./a.out std::accumulate (double) sum: 10,000,000.7 time: 356.9 ms std::reduce (seq, double) sum: 10,000,000.7 time: 140.1 ms std::reduce (par, double) sum: 10,000,000.7 time: 140.1 ms std::accumulate (long) sum: 100,000,007 time: 46.0 ms std::reduce (seq, long) sum: 100,000,007 time: 67.3 ms std::reduce (par, long) sum: 100,000,007 time: 63.3 ms // POSIX: g++ -std=c++23 ./example.cpp -ltbb -O3 -DPARALLEL; ./a.out std::accumulate (double) sum: 10,000,000.7 time: 353.4 ms std::reduce (seq, double) sum: 10,000,000.7 time: 140.7 ms std::reduce (par, double) sum: 10,000,000.7 time: 24.7 ms std::accumulate (long) sum: 100,000,007 time: 42.4 ms std::reduce (seq, long) sum: 100,000,007 time: 52.0 ms std::reduce (par, long) sum: 100,000,007 time: 23.1 ms
[編輯] 另請參閱
對一個範圍的元素進行求和或摺疊 (函式模板) | |
對一個範圍的元素應用函式,並將結果儲存在目標範圍中 (函式模板) | |
(C++17) |
應用一個可呼叫物件,然後進行亂序歸約 (函式模板) |
(C++23) |
對一個範圍的元素進行左摺疊 (演算法函式物件) |