C++STL 求和:accumulate 【转】
该算法在numeric头文件中定义。
accumulate()的原型为(文件取自DEV-C++编译器):
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
_BinaryOperation __binary_op)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_requires_valid_range(__first, __last); for ( ; __first != __last; ++__first)
__init = __binary_op(__init, *__first);
return __init;
}
假设vec是一个int型的vector对象,下面的代码:
//sum the elements in vec starting the summation with the value 42
int sum = accumulate(vec.begin() , vec.end() , );
将sum设置为vec的元素之和再加上42。
accumulate带有三个形参:头两个形参指定要累加的元素范围,第三个形参则是累加的初值。
accumulate函数将它的一个内部变量设置为指定的初始值,然后在此初值上累加输入范围内所有元素的值。accumulate算法返回累加的结果,其返回类型就是其第三个实参的类型。
用于指定累加起始值的第三个参数是必要的,因为accumulate对将要累加的元素类型一无所知,除此之外,没有别的办法创建合适的起始值或者关联的类型。
accumulate对要累加的元素类型一无所知,这个事实有两层含义。首先,调用该函数时必需传递一个初始值,否则,accumulate将不知道使用什么初始值。其次,容器内的元素类型必须与第三个实参的类型匹配,或者可转换为第三个实参的类型。在accumulate内部,第三个实参用作累加的起点;容器内的元素按顺序连续累加到综合之中。因此,必须能够将元素类型加到总和类型上。
假定V是vector<double>类型的对象,则调用accumulate(v.begin() , v.end() , 0)是否有错?如果有的话,错在哪里?
从函数调用上看没有错误。
调用accumulate函数必须满足的条件包括:容器内的元素类型必须与第三个实参的类型匹配,或者可转换为第三个实参的类型。上述调用中的第三个实参为int类型,而vector对象中的元素的类型为double类型,可以转换为int类型。
但计算的结果不准确。因为将double类型转换为int类型会截去小数部分,得到的求和结果是各元素的整数部分的和,是一个int类型的值,与实际的元素值总和相比会有比较大的误差。
考虑下面的例子,可以使用accumulate把string型的vector容器中的元素连接起来:
//concatenate elements from V and store in sum
string sum = accumulate(v.begin() , v.end() , string(" "));
这个函数调用的效果是:从空字符串开始,把vec里的每个元素连接成一个字符串。
下面让我们用一个具体事例来说明:用accumulate统计vector<int>容器对象中的元素之和。
//读取一系列int型数据,并将它们存储到vector对象中,
//然后使用algorithm头文件中定义的名为accumulate的函数,
//统计vector对象中的元素之和
#include<iostream>
#include<vector>
#include<numeric>
using namespace std; int main()
{
int ival;
vector<int> ivec; //读入int型数据并存储到vector对象中,直至遇到文件结束符
cout<<"Enter some integers(Ctrl+z to end): "<<endl;
while(cin >> ival)
ivec.push_back(ival); //使用accumulate函数统计vector对象中的元素之和并输出结果
cout<<"summation of elements in the vector: "
<<accumulate(ivec.begin() , ivec.end() , ) //统计vector对象中的元素之和
<<endl; return ;
}
accumulate()可以用来直接计算数组或者容器中C++内置数据类型,但是对于自定义数据类型,我们就需要自己动手写一个类来实现自定义数据的处理,然后让它作为accumulate()的第四个参数。
假设自定义数据类型为:
struct Student
{
string name; // 学生姓名
int total; // 四级分数
};
那么我们可能要定义如下列的类:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
using namespace std; struct Student
{
string name;
int total;
}; class PS{
public:
int operator()(int t1,const Student& t2)
{
return (t1 + t2.total);
} }; int main()
{
Student student[]={
{"hicjiajia",},
{"sijikaoshi",},
{"what",}
}; int sum=accumulate(&student[],&student[],,PS());
cout<<sum<<endl; system("pause");
return ;
}
文末附:资料链接
最新文章
- Windows Server 2012 虚拟化实战:SCVMM的安装和部署
- uwsgi出现invalid request block size: 21573 (max 4096)...skip解决办法
- weblogic启动错误
- Eclipse快捷键(转)
- App Store上下载和安装Xcode
- codeforces 22E XOR on Segment 线段树
- 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼
- GitHub命令精简教程
- The xor-longest Path
- Sublime Text2 快捷键设置
- [C#学习]1.Hello World
- The Balance
- 《android开发进阶从小工到专家》读书笔记--网络框架的设计与实现
- [JLOI2015]城池攻占
- Elasticsearch-6.7.0系列(一)9200端口 .tar.gz版本centos7环境--下载安装运行
- URL 与 URI 介绍
- lucene学习教程
- selenium之调用Javascript
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
- HDU 5835 Danganronpa 贪心