STL --------- common arithmetic generation algorithm

Common arithmetic generation algorithm

(1) requires the header file
(2) accumulate (begin, end , 0): calculated data and the container, the third parameter is the initial accumulated value;
(. 3) Fill (the begin, End, 1000): 1000 filled, to start defining when resize (10,0) is filled with 0;

Code Analysis

#include<iostream>
#include<list>
#include<iterator>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
//---------------------------------------------------------------------------
//accumulate 累加
void test01()
{
	list<int > l;
	for(int i=0;i<=100;i++)
	{
		l.push_back(i);
	}

	int sum=accumulate(l.begin(),l.end(),0);//0+容器总和
	cout<<sum<<endl;

	int sum1=accumulate(l.begin(),l.end(),1000);//1000+容器总和
	cout<<sum1<<endl;
}
//--------------------------------------------------------------------------------
//fill 填充
void test02()
{
	list<int> l;
	l.resize(10,0); //在初始化时,resize()也可以填充
	copy(l.begin(),l.end(),ostream_iterator<int>(cout," "));
	cout<<endl;

	fill(l.begin(),l.end(),1000);//后面填充用fill;
	copy(l.begin(),l.end(),ostream_iterator<int>(cout," "));
	cout<<endl;
}
int main()
{
	test02();
	//test01();
	return 0;
}
Published 52 original articles · won praise 14 · views 5573

Guess you like

Origin blog.csdn.net/YanWenCheng_/article/details/104088182