C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
- æå¶çº¦ç®æ³ (C++20 èµ·)
- std::is_execution_policy
- std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
- std::all_of, std::any_of, std::none_of
- std::for_each_n
- std::sort
- std::reduce
- std::inclusive_scan
- std::exclusive_scan
- std::random_shuffle, std::shuffle
- std::clamp
- std::equal
- std::is_permutation
- std::mismatch
- std::swap
- std::search
- std::transform
- std::for_each
- std::make_heap
- std::count, std::count_if
- std::adjacent_find
- std::find, std::find_if, std::find_if_not
- std::find_end
- std::find_first_of
- std::search_n
- std::lexicographical_compare
- std::lexicographical_compare_three_way
- std::copy, std::copy_if
- std::copy_n
- std::copy_backward
- std::move
- std::move_backward
- std::shift_left, std::shift_right
- std::fill
- std::fill_n
- std::generate
- std::generate_n
- std::iter_swap
- std::swap_ranges
- std::sample
- std::remove, std::remove_if
- std::replace, std::replace_if
- std::reverse
- std::rotate
- std::unique
- std::remove_copy, std::remove_copy_if
- std::replace_copy, std::replace_copy_if
- std::reverse_copy
- std::rotate_copy
- std::unique_copy
- std::is_partitioned
- std::partition_point
- std::partition
- std::partition_copy
- std::stable_partition
- std::is_sorted
- std::is_sorted_until
- std::stable_sort
- std::partial_sort
- std::partial_sort_copy
- std::nth_element
- std::lower_bound
- std::upper_bound
- std::binary_search
- std::equal_range
- std::merge
- std::inplace_merge
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::includes
- std::is_heap
- std::is_heap_until
- std::sort_heap
- std::push_heap
- std::pop_heap
- std::max
- std::max_element
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::next_permutation
- std::prev_permutation
- std::iota
- std::inner_product
- std::adjacent_difference
- std::accumulate
- std::transform_reduce
- std::partial_sum
- std::transform_inclusive_scan
- std::transform_exclusive_scan
- std::qsort
- std::bsearch
- 注é
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >ç®æ³åº > std::reduce
std::reduce
| å®ä¹äºå¤´æä»¶ <numeric>
|
||
| (1) | ||
| template<class InputIt> typename std::iterator_traits<InputIt>::value_type reduce( |
(C++17 èµ·) (C++20 å) |
|
| template<class InputIt> constexpr typename std::iterator_traits<InputIt>::value_type reduce( |
(C++20 èµ·) | |
| template<class ExecutionPolicy, class ForwardIt> typename std::iterator_traits<ForwardIt>::value_type reduce( |
(2) | (C++17 èµ·) |
| (3) | ||
| template<class InputIt, class T> T reduce(InputIt first, InputIt last, T init); |
(C++17 èµ·) (C++20 å) |
|
| template<class InputIt, class T> constexpr T reduce(InputIt first, InputIt last, T init); |
(C++20 èµ·) | |
| template<class ExecutionPolicy, class ForwardIt, class T> T reduce(ExecutionPolicy&& policy, |
(4) | (C++17 èµ·) |
| (5) | ||
| template<class InputIt, class T, class BinaryOp> T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); |
(C++17 èµ·) (C++17 å) |
|
| template<class InputIt, class T, class BinaryOp> constexpr T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); |
(C++17 èµ·) | |
| template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp> T reduce(ExecutionPolicy&& policy, |
(6) | (C++17 èµ·) |
binary_op ä¸ä»¥åå¼ init è§çº¦èå´ [first; last) ï¼å¯è½ä»¥æªæå®æ¹å¼æåºèåã policy æ§è¡ãæ¤éè½½ä»
è¥std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 trueæåä¸éè½½å³è®®è¥ binary_op éç»åæé交æ¢ï¼åè¡ä¸ºéç¡®å®ã
è¥ binary_op ä¿®æ¹ [first; last] ä¸ä»»ä½å
ç´ æéæ³åèå´ä¸ä»»ä½è¿ä»£å¨ï¼å«å°¾è¿ä»£å¨ï¼åè¡ä¸ºæªå®ä¹ã
åæ°
| first, last | - | è¦åºç¨ç®æ³çå ç´ èå´ |
| init | - | 广ä¹åçåå¼ |
| policy | - | 使ç¨çæ§è¡çç¥ãç»èè§æ§è¡çç¥ã |
| binary_op | - | å°ä»¥æªæå®é¡ºåºåºç¨äºè§£å¼ç¨è¾å
¥è¿ä»£å¨ç»æãå
¶ä» binary_op ç»æå init ä¸çäºå
彿°å¯¹è±¡ (FunctionObject) ã
|
| ç±»åè¦æ± | ||
-InputIt å¿
须满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) çè¦æ±ã
| ||
-ForwardIt å¿
须满足éçååè¿ä»£å¨ (LegacyForwardIterator) çè¦æ±ã
| ||
-T å¿
须满足å¯ç§»å¨æé (MoveConstructible) çè¦æ±ãèä¸ binary_op(init, *first) ã binary_op(*first, init) ã binary_op(init, init) å binary_op(*first, *first) å¿
é¡»å¯è½¬æ¢å° T ã
| ||
è¿åå¼
init å *first ã *(first+1) ãâ¦â¦ *(last-1) å¨ binary_op ä¸ç广ä¹åï¼
å
¶ä¸å¹¿ä¹å GSUM(op, a
1, ..., a
N) å®ä¹å¦ä¸ï¼
- è¥ N=1 ï¼å为 a
1 - è¥ N > 1 ï¼å为 op(GSUM(op, b
1, ..., b
K), GSUM(op, b
M, ..., b
N)) ï¼å ¶ä¸
- b
1, ..., b
N å¯ä»¥æ¯ä»»ä½ a1, ..., aN çæåï¼ä¸ - 1 < K+1 = M ⤠N
- b
æ¢è¨ä¹ï¼ reduce 表ç°ç±»ä¼¼ std::accumulate ï¼é¤äºèå´ä¸çå
ç´ å¯è½ä»¥ä»»æé¡ºåºåç»å¹¶éæã
å¤æåº¦
O(last - first) 次åºç¨ binary_op.
å¼å¸¸
æ¥æå为 ExecutionPolicy çæ¨¡æ¿å½¢åçéè½½æä¸åæ¹å¼æ¥åé误ï¼
- è¥ä½ä¸ºç®æ³ä¸é¨åè°ç¨ç彿°çæ§è¡æåºå¼å¸¸ï¼ä¸
ExecutionPolicy为æ åçç¥ä¹ä¸ï¼åè°ç¨ std::terminate ã对äºä»»ä½å ¶ä»ExecutionPolicyï¼è¡ä¸ºæ¯å®ç°å®ä¹çã - è¥ç®æ³æ æ³åé å åï¼åæåº std::bad_alloc ã
注解
è¥èå´ä¸ºç©ºï¼åè¿åä¸ä¿®æ¹ç init ã
示ä¾
reduce ä¸ std::accumulate é´å¹¶è¡çæ¯è¾ï¼
#include <iostream> #include <chrono> #include <vector> #include <numeric> #include <execution> Â int main() { std::vector<double> v(10'000'007, 0.5); Â { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::accumulate(v.begin(), v.end(), 0.0); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n"; } Â { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::reduce(std::execution::par, v.begin(), v.end()); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << "std::reduce result " << result << " took " << ms.count() << " ms\n"; } }
å¯è½çè¾åºï¼
std::accumulate result 5000003.50000 took 12.7365 ms std::reduce result 5000003.50000 took 5.06423 ms
åé
| 对ä¸ä¸ªèå´å
çå
ç´ æ±å (彿°æ¨¡æ¿) | |
| å°ä¸ä¸ªå½æ°åºç¨äºæä¸èå´çå个å
ç´ ï¼å¹¶å¨ç®æ èå´åå¨ç»æ (彿°æ¨¡æ¿) | |
| (C++17) |
åºç¨ä¸ä¸ªå½æ°å¯¹è±¡ï¼ç¶å以乱åºè§çº¦ (彿°æ¨¡æ¿) |