C++ Primer 第11章 习题11.3

//11.3.cpp
//用accumulate统计vector<int>容器对象中的元素之和
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;

int main()
{
	vector<int> ivec;
	int ival;
	int sum;
	//读入int型元素带vector对象中
	cout<<"Enter some integers(Ctrl+z to end):"<<endl;
	while(cin>>ival)
		ivec.push_back(ival);

	//调用accumulate函数返回vector容器对象中元素之和
	sum=accumulate(ivec.begin(),ivec.end(),0);
	cout<<"summation of elements in the vector:"
		<<sum<<endl;

	return 0;
}

posted on 2012-03-01 17:07  1.曲待续  阅读(104)  评论(0编辑  收藏  举报

导航