本文介绍: 【代码】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;
}

在这里插入图片描述

原文地址:https://blog.csdn.net/qq_45159887/article/details/134656048

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_1150.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

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