c++之std::accumulate连续操作如累加累积等
来源:互联网 发布:有哪些网络电话软件 编辑:程序博客网 时间:2024/06/05 04:40
#include <iostream>#include <functional> #include <numeric>#include <string>#include <vector>using namespace std;//http://www.cplusplus.com/reference/numeric/accumulate/// accumulate example//100 10 20 30=>100+10*2+20*2+30*2=220int myfunction(int x, int y){int ret = x + 2 * y;return ret;}struct myclass {//100 10 20 30=>100+10*3+20*3+30*3=280int operator()(int x, int y) { int ret = x + 3 * y;return ret; }} myobject;int test_accumulate002() {int init = 100;int numbers[] = { 10, 20, 30 };std::cout << "using default accumulate: ";std::cout << std::accumulate(numbers, numbers + 3, init);//初始值为init=100 累加std::cout << '\n';std::cout << "using functional's minus: ";std::cout << std::accumulate(numbers, numbers + 3, init, std::minus<int>());//初始值为init=100 连续减std::cout << '\n';std::cout << "using custom function: ";std::cout << std::accumulate(numbers, numbers + 3, init, myfunction);//初始值为init=100 元素间连续执行myfunction操作std::cout << '\n';std::cout << "using custom object: ";std::cout << std::accumulate(numbers, numbers + 3, init, myobject);//初始值为init=100 元素间连续执行myobject ()操作std::cout << '\n';return 0;}//http://en.cppreference.com/w/cpp/algorithm/accumulate//c++之std::accumulate连续操作如累加累积等//template <class InputIterator, class T> T accumulate(InputIterator first, InputIterator last, T init)//连续操作,默认操作是添加的元素,但不同的操作可以被指定为binary_op。int main(int argc, char const *argv[]){int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };size_t size = sizeof(arr) / sizeof(arr[0]);cout <<"连加="<<std::accumulate(arr, arr + size, 0) << endl;//累积 连加cout <<"连乘="<< std::accumulate(arr, arr + size,1,// 累加器初始值。multiplies<int>()) << endl;//累积 连乘vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };std::string s = std::accumulate(std::next(v.begin()), v.end(),std::to_string(v[0]), // start with first element 累加器初始值。[](std::string a, int b) {return a + '-' + std::to_string(b);});cout << "链接=" << s << endl;test_accumulate002();system("pause");return 0;}
0 0
- c++之std::accumulate连续操作如累加累积等
- c++STL之accumulate
- 算法---累加/累积
- std::transform || inner_product || accumulate
- [C++] vector 之 accumulate 用法示例
- JS常见算法-累加/累积
- std::accumulate(容器求和)
- js 针对html 元素 css样式 操作 等 累积
- c++ std - accumulate 求数组和
- STL之accumulate
- accumulate
- accumulate
- accumulate
- C知识点累积篇
- C/C++ 知识累积。
- EXTJS Grid 之ActionColumn ,添加最后 操作列,如编辑、下载、删除等。
- C++新手之accumulate函数
- 经典造轮子之accumulate
- 项目接触到的业务流程
- 【video】FLV格式
- HTTP AJAX POST请求
- poj Candies(差分约束,spfa栈)
- Markdown入门指南
- c++之std::accumulate连续操作如累加累积等
- 20 : Set Matrix Zeroes
- 红黑树
- 51nod 1239欧拉函数之和
- linux内核剖析
- 利用javascript计算n阶矩阵
- 在Linux服务器下修改mysql编码
- Spring Boot RabbitMQ 入门(三)之 Fanout交换器
- map 用法详解(转)