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::accumulate
std::accumulate
| å®ä¹äºå¤´æä»¶ <numeric>
|
||
| (1) | ||
| template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init ); |
(C++20 å) | |
| template< class InputIt, class T > constexpr T accumulate( InputIt first, InputIt last, T init ); |
(C++20 èµ·) | |
| (2) | ||
| template< class InputIt, class T, class BinaryOperation > T accumulate( InputIt first, InputIt last, T init, |
(C++20 å) | |
| template< class InputIt, class T, class BinaryOperation > constexpr T accumulate( InputIt first, InputIt last, T init, |
(C++20 èµ·) | |
计ç®ç»å®å¼ init ä¸ç»å®èå´ [first, last) ä¸å
ç´ çåã第ä¸çæ¬ç¨ operator+ ï¼ç¬¬äºçæ¬ç¨äºå
彿° op æ±åå
ç´ ï¼åå° std::move åºç¨å°å
¶å·¦ä¾§è¿ç®æ° (C++20 èµ·)ã
|
|
(C++11 å) |
|
|
(C++11 èµ·) |
åæ°
| first, last | - | è¦æ±åçå ç´ èå´ |
| init | - | åçåå¼ |
| op | - | 被使ç¨çäºå
彿°å¯¹è±¡ãæ¥æ¶å½åç§¯ç´¯å¼ a ï¼åå§å为 init ï¼åå½åå
ç´ b çäºå
è¿ç®ç¬¦ãè¯¥å½æ°çç¾ååºå½çä»·äºï¼  Ret fun(const Type1 &a, const Type2 &b); ç¾åä¸å¹¶ä¸éè¦æ const &ã |
| ç±»åè¦æ± | ||
-InputIt å¿
须满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) çè¦æ±ã
| ||
-T å¿
须满足å¯å¤å¶èµå¼ (CopyAssignable) å å¯å¤å¶æé (CopyConstructible) çè¦æ±ã
| ||
è¿åå¼
注解
std::accumulate è¿è¡å·¦æå ã为è¿è¡å³æå ï¼å¿
é¡»é转äºå
è¿ç®ç¬¦çåæ°é¡ºåºï¼å¹¶ä½¿ç¨éåºè¿ä»£å¨ã
å¯è½çå®ç°
| çæ¬ä¸ |
|---|
template<class InputIt, class T> constexpr // C++20 èµ· T accumulate(InputIt first, InputIt last, T init) { for (; first != last; ++first) { init = std::move(init) + *first; // C++20 èµ·æ std::move } return init; } |
| çæ¬äº |
template<class InputIt, class T, class BinaryOperation> constexpr // C++20 èµ· T accumulate(InputIt first, InputIt last, T init, BinaryOperation op) { for (; first != last; ++first) { init = op(std::move(init), *first); // C++20 èµ·æ std::move } return init; } |
示ä¾
#include <iostream> #include <vector> #include <numeric> #include <string> #include <functional>  int main() { std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  int sum = std::accumulate(v.begin(), v.end(), 0);  int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());  auto dash_fold = [](std::string a, int b) { return std::move(a) + '-' + std::to_string(b); };  std::string s = std::accumulate(std::next(v.begin()), v.end(), std::to_string(v[0]), // ç¨é¦å ç´ å¼å§ dash_fold);  // 使ç¨éåè¿ä»£å¨å³æå std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(), std::to_string(v.back()), // ç¨é¦å ç´ å¼å§ dash_fold);  std::cout << "sum: " << sum << '\n' << "product: " << product << '\n' << "dash-separated string: " << s << '\n' << "dash-separated string (right-folded): " << rs << '\n'; }
è¾åºï¼
sum: 55 product: 3628800 dash-separated string: 1-2-3-4-5-6-7-8-9-10 dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1
åé
| 计ç®èå´å
åç¸é»å
ç´ ä¹é´çå·® (彿°æ¨¡æ¿) | |
| 计ç®ä¸¤ä¸ªèå´çå
ç´ çå
积 (彿°æ¨¡æ¿) | |
| 计ç®èå´å
å
ç´ çé¨åå (彿°æ¨¡æ¿) | |
| (C++17) |
类似 std::accumulateï¼ä½ä¸ä¾åºæ§è¡ (彿°æ¨¡æ¿) |