C++常用的算术生成算法

C++常用的算术生成算法

学习目标:

掌握常用的算术生成算法

注意:

算术生成算法属于小型算法,使用时包含的头文件为#include

算法简介:

accumulate   //计算容器元素累计总和
fill       //向容器中添加元素

accumulate

功能描述:
*
计算区间内容器元素累计总和

函数原型:

accumulate(iterator beg, iterator end, value);

//计算容器元素累计总和

//beg开始迭代器

//end结束迭代器

//value起始值

代码示例:

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
void test01()
{
   
       vector<int>v;
       for (int i = 0; i <= 100; i++)
       {
   
              v.push_back(i);
       }
       //参数3 起始累加值
       int total =accumulate(v.begin(), v.end(), 1000);
       cout << "total=" << total << endl;
}
int main()
{
   
       test01();
       system("pause");
       return 0;
}

总结: accumulate使用时头文件注意是numeric,这个算法很实用

C/C++基础 文章被收录于专栏

本专栏收录C/C++编程语言相关知识

全部评论

相关推荐

积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务