//为了方便,要包含的文件会不同,但我没有删
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
#include <cstring> //和C语言的string.h其实是一个版本
#include <stdexcept> //或用 #include <exception> 都行
using std::bitset;
using std::vector;
using std::cin;
using std::endl;
using std::cout;
using std::string;
using std::overflow_error;
//#define NDEBUG
//---------------------------------------------------------------------------
double getTotal(vector<double>::const_iterator beg,vector<double>::const_iterator end)
{
double t=0;
while(beg!=end)
{
t+=*beg;
++beg;
}
return t;
}
int main()
{
//建立vector
cout<<"请输入多个小数用于建立vector:"<<endl;
vector<double> dvec;
double d;
while(cin>>d)
dvec.push_back (d);
//
//求数组之和
double k=getTotal(dvec.begin (),dvec.end ());
cout<<"它们的和是:"<<k<<endl;
return 0;
}
/*
210页习题7.14求vector<double> 对象中所有元素之和
?vector<double>::const_iterator beg 和 const vector<double>::iterator beg之间有些什么区别呢?
当我使用后一种是不行的,因为说++beg里的++无法跟后面的操作数,那么问题就在const符号上了。
因为后一种为const指针,指针是不能改变的,而前一种为指向const对象的指针
因为iterator本身为指针,前加const后,就是用const修饰指针,所以是const指针了
如定义指向const对象的指针:const int* pc; /const int *pc;
指向普通对象的const指针:int* const cp; /int *const cp;
指向const对象的const指针:const int* const cpp; /const int *const cpp;
*/