c++numeric


title: c++numeric
categories: 编程
tags:

  • c++
    date: 2022-04-06 15:26:13

numeric针对数字序列的头文件

numeric.png

#include

针对某些数字序列的操作,需要用到较多的二元谓词

示例

准备

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

void PrintV(int v)
{
	cout << v << " ";
}

void show(vector<int> v)
{
	for_each(v.begin(), v.end(), PrintV);
	cout << endl;
}

客户端代码

int main()
{
	vector<int> v;//示例容器
	vector<int> new_v;//复制容器
	vector<int> reunion;//合并容器
	reunion.resize(20);
	vector<int>::iterator it;//返回迭代器
	for (int i = 1; i < 10; i++)
	{
		v.push_back(i);
		new_v.push_back(i * 10);
	}
	int result = 0; //存放结果
	
	int sum = 0;
	result = accumulate(v.begin(), v.end(), sum, minus<int>());//累积
	cout << result << endl;

	show(new_v);
	show(v);
	result = inner_product(new_v.begin(), new_v.end(),v.begin(), sum, plus<int>(),divides<int>());//前面的是变量,第二个是参数,然后先除,再累加到sum上面
	cout << result << endl;

	adjacent_difference(new_v.begin(), new_v.end(), reunion.begin(),minus<int>());
	show(reunion);//对两个之间进行操作

	partial_sum(v.begin(), v.end(), reunion.begin());
	show(reunion);//类似求前n项和

	cout << "end" << endl;
	return 0;
}

结果

-45
10 20 30 40 50 60 70 80 90
1 2 3 4 5 6 7 8 9
90
10 10 10 10 10 10 10 10 10 0 0 0 0 0 0 0 0 0 0 0
1 3 6 10 15 21 28 36 45 0 0 0 0 0 0 0 0 0 0 0
end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值