美文网首页
C++ accumulate()函数

C++ accumulate()函数

作者: TFprime | 来源:发表于2019-03-07 16:33 被阅读0次

作用

accumulate函数将一段数字从头到尾累加起来,或者使用指定的运算符进行运算
accumulate函数的前两个参数指定累加的范围,第三个参数为累加的初值,第四个参数为进行的操作,默认为累加
使用accumulate要添加#include<numeric>
accumulate函数声明

template<class InputIterator, class Type>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val
   );
template<class InputIterator, class Type, class Fn2>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val, 
      BinaryOperation _Binary_op //自定义二进制操作
   );

举例

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;

int main() {

    vector<int> nums = {1, 2, 3, 4, 5};
    int result = accumulate(nums.begin(), nums.end(), 0);
    cout << result << endl;
    
    return 0;
}

Output:
result = 15

相关文章

  • C++ accumulate()函数

    作用 accumulate函数将一段数字从头到尾累加起来,或者使用指定的运算符进行运算accumulate函数的前...

  • C++ STL accumulate 使用说明

    说明 std::accumulate 用于对容器[first,last)内的元素和初始值init进行累积运算。 前...

  • windows逆向3

    VC 程序内存和编译的一些特征C++ 构造函数C++ 成员函数C++ 析构函数C++ 全局对象的构造C++ 全局对...

  • C++ STL bit_xor 使用说明

    1. 说明 异或的函数对象类。可用作 transform 或者 accumulate等的操作符。 类似的还有与操作...

  • C++Primer之 函数探幽

    读C++ primer总结 C++函数包括函数声明和函数定义,函数声明即函数原型,一般隐藏在include文件中。...

  • 一些函数

    cmp函数 C++ sort cmp函数 - lzz的博客 - CSDN博客 浅谈C/C++排序函数中cmp()比...

  • iOS-底层(11):dyld加载流程

    +load方法、c++函数、main函数的调用顺序 从打印顺序我们可以看到:** +load方法 -> c++函数...

  • 聚集accumulate

    accumulate、amass、collect、gather、heap与pile 2019年8月12日 口语与词...

  • python程序员需要掌握的单词——持续更新

    accumulate 积累 mutable...

  • C/C++ 函数地址

    C 函数 C 语言中没有类的概念,只有普通的函数。 控制台输出: C++ 函数 C++ 函数有如下几种: 1)普通...

网友评论

      本文标题:C++ accumulate()函数

      本文链接:https://www.haomeiwen.com/subject/qxfhpqtx.html