std::accumulate | 您所在的位置:网站首页 › accumulate名次 › std::accumulate |
C++
语言
标准库头文件
自立与有宿主实现
具名要求
语言支持库
概念库 (C++20)
诊断库
工具库
字符串库
容器库
迭代器库
范围库 (C++20)
算法库
数值库
本地化库
输入/输出库
文件系统库 (C++17)
正则表达式库 (C++11)
原子操作库 (C++11)
线程支持库 (C++11)
技术规范
算法库
有制约算法及范围上的算法 (C++20)
有制约算法: std::ranges::copy, std::ranges::sort, ...
执行策略 (C++17)
is_execution_policy(C++17)
execution::seqexecution::parexecution::par_unseqexecution::unseq(C++17)(C++17)(C++17)(C++20)
execution::sequenced_policyexecution::parallel_policyexecution::parallel_unsequenced_policyexecution::parallel_unsequenced(C++17)(C++17)(C++17)(C++20)
不修改序列的操作
all_ofany_ofnone_of(C++11)(C++11)(C++11)
for_each
for_each_n(C++17)
countcount_if
mismatch
equal
adjacent_find
findfind_iffind_if_not(C++11)
find_end
find_first_of
adjacent_find
search
search_n
lexicographical_compare
lexicographical_compare_three_way(C++20)
修改序列的操作
copycopy_if(C++11)
copy_n(C++11)
copy_backward
move(C++11)
move_backward(C++11)
shift_leftshift_right(C++20)(C++20)
transform
fill
fill_n
generate
generate_n
swap
iter_swap
swap_ranges
sample(C++17)
removeremove_if
replacereplace_if
reverse
rotate
unique
random_shuffle(C++17 前)
remove_copyremove_copy_if
replace_copyreplace_copy_if
reverse_copy
rotate_copy
unique_copy
shuffle(C++11)
未初始化存储上的操作
uninitialized_copy
uninitialized_move(C++17)
uninitialized_fill
destroy(C++17)
uninitialized_copy_n(C++11)
uninitialized_move_n(C++17)
uninitialized_fill_n
destroy_n(C++17)
uninitialized_default_construct(C++17)
uninitialized_value_construct(C++17)
destroy_at(C++17)
uninitialized_default_construct_n(C++17)
uninitialized_value_construct_n(C++17)
construct_at(C++20)
划分操作
is_partitioned(C++11)
partition_point(C++11)
partition
partition_copy(C++11)
stable_partition
排序操作
is_sorted(C++11)
is_sorted_until(C++11)
sort
stable_sort
partial_sort
partial_sort_copy
nth_element
二分搜索操作
lower_bound
upper_bound
binary_search
equal_range
集合操作(在已排序范围上)
merge
inplace_merge
set_difference
set_intersection
set_symmetric_difference
set_union
includes
堆操作
is_heap(C++11)
is_heap_until(C++11)
make_heap
sort_heap
push_heap
pop_heap
最小/最大操作
max
max_element
min
min_element
minmax(C++11)
minmax_element(C++11)
clamp(C++17)
排列
is_permutation(C++11)
next_permutation
prev_permutation
数值运算
iota(C++11)
inner_product
adjacent_difference
accumulate
reduce(C++17)
transform_reduce(C++17)
partial_sum
inclusive_scan(C++17)
exclusive_scan(C++17)
transform_inclusive_scan(C++17)
transform_exclusive_scan(C++17)
C 库
qsort
bsearch
定义于头文件
(1)
template
T accumulate( InputIt first, InputIt last, T init );
(C++20 前)
template
constexpr T accumulate( InputIt first, InputIt last, T init );
(C++20 起)
(2)
template
T accumulate( InputIt first, InputIt last, T init, BinaryOperation op ); (C++20 前) templateconstexpr T accumulate( InputIt first, InputIt last, T init, BinaryOperation op ); (C++20 起)计算给定值 init 与给定范围 [first, last) 中元素的和。第一版本用 operator+ ,第二版本用二元函数 op 求和元素,均将 std::move 应用到其左侧运算数 (C++20 起)。 op 必须无副作用。 (C++11 前)op 必须不非法化涉及范围的任何迭代器,含尾迭代器,且不修改其所涉及范围的任何元素及 *last 。 (C++11 起) 参数 first, last - 要求和的元素范围 init - 和的初值 op - 被使用的二元函数对象。接收当前积累值 a (初始化为 init )和当前元素 b 的二元运算符。该函数的签名应当等价于: Ret fun(const Type1 &a, const Type2 &b); 签名中并不需要有 const &。类型 Type1 必须使得 T 类型的对象能隐式转换到 Type1 。类型 Type2 必须使得 InputIt 类型的对象能在解引用后隐式转换到 Type2 。 类型 Ret 必须使得 T 类型对象能被赋 Ret 类型值。 类型要求 -InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 -T 必须满足可复制赋值 (CopyAssignable) 和 可复制构造 (CopyConstructible) 的要求。 返回值 1) 给定值与给定范围中的元素的和。 2) 给定范围在 op 上左折叠的结果 注解std::accumulate 进行左折叠。为进行右折叠,必须逆转二元运算符的参数顺序,并使用逆序迭代器。 最常见的错误在 init 的类型上进行 op ,这能引入不期望的元素转换,例如 std::accumulate(v.begin(), v.end(), 0) 在 v 为 std::vector 时给出错误的结果。 可能的实现 版本一 template 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 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 #include #include #include #include int main() { std::vector 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()); 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 |
CopyRight 2018-2019 实验室设备网 版权所有 |