STL算法-accumlate
来源:互联网 发布:windows死循环脚本 编辑:程序博客网 时间:2024/05/07 06:31
包含在头文件numeric 中
accumlate 源码:
template<class InputIt, class T>T accumulate(InputIt first, InputIt last, T init){ for (; first != last; ++first) { init = init + *first; } return init;}
template<class InputIt, class T, class BinaryOperation>T accumulate(InputIt first, InputIt last, T init, BinaryOperation op){ for (; first != last; ++first) { init = op(init, *first); } return init;}
#include <iostream>#include <vector>#include <numeric>#include <string> int multiply(int x, int y){ return x*y;} std::string magic_function(std::string res, int x){ return res += (x > 5) ? "b" : "s";} 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, multiply); std::string magic = std::accumulate(v.begin(), v.end(), std::string(), magic_function); std::cout << sum << '\n' << product << '\n' << magic << '\n';}
结果:
553628800sssssbbbbb
0 0
- STL算法-accumlate
- STL算法
- STL 算法
- STL 算法
- STL-算法
- STL算法
- STL 算法
- STL算法
- STL算法
- STL 算法
- STL 算法
- STL算法
- stl算法
- Stl算法
- STL算法
- STL 算法
- STL 算法
- STL 算法
- 1385: [Baltic2000]Division expression
- 中的那一抹温柔
- 1968: [Ahoi2005]COMMON 约数研究
- WCF开发之服务契约
- 1045: [HAOI2008] 糖果传递
- STL算法-accumlate
- 2463: [中山市选2009]谁能赢呢?
- 快速排序_C语言
- 1800: [Ahoi2009]fly 飞行棋
- BZOJ 3524: [Poi2014]Couriers
- mac下查看所有用户和组
- Redis连接实例
- 1196: [HNOI2006]公路修建问题
- 通过jQuery源码学习javascript