本文介绍: 【代码】C++之算术生成算法

C++之算术生成算法

在这里插入图片描述

accumulate

在这里插入图片描述

#include<iostream&gt;
using namespace std;
#include<vector>
#include<numeric>

void test()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	int total = accumulate(v.begin(), v.end(),0);
	cout << total << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

fill

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>

class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val<<" ";
	}
};

void test()
{
	vector<int> v;
	v.resize(10);
	cout << "默认值" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	fill(v.begin(), v.end(), 100);
	cout << "从新填充" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注