【C++】学习笔记[二十二]

STL常用算法【算术生成和集合】

1、accumulate

  • 计算区间内容器元素累计总和
  • 需要加头文件#include <numeric>

练习代码:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1);
	}

	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;

	cout << "total = " << accumulate(v1.begin(), v1.end(),0) << endl;//第三个参数是起始累加值
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

2、fill

  • 向容器中填充指定元素

练习代码:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	v1.resize(10);

	cout << "填充前:" << endl;
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;

	cout << "填充后:" << endl;
	fill(v1.begin(), v1.end(), 100);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

3、set_intersection

  • 求两个容器交集
  • 必须是两个有序序列

练习代码:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1, v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);//0-9
		v2.push_back(i + 5);//5-14
	}

	vector<int> vt;//目标容器
	//需要提前开辟空间,开辟空间取小容器的值即可
	vt.resize(min(v1.size(), v2.size()));
	vector<int>::iterator it = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vt.begin());//返回值是交集中最后一个位置

	for_each(vt.begin(), it, myPrint);
	cout << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

4、set_union

  • 求两个容器并集
  • 必须是两个有序序列

练习代码:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1, v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);//0-9
		v2.push_back(i + 5);//5-14
	}

	vector<int> vt;//目标容器
	//需要提前开辟空间,开辟空间取两个容器大小的值即可
	vt.resize(v1.size()+v2.size());
	vector<int>::iterator it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vt.begin());

	for_each(vt.begin(), it, myPrint);
	cout << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

5、set_difference

  • 求两个容器差集
  • 必须是两个有序序列

差集解释:

在这里插入图片描述

练习代码:

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

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1, v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);//0-9
		v2.push_back(i + 5);//5-14
	}

	vector<int> vt;//目标容器
	//需要提前开辟空间,开辟空间取两个容器较大的size的值即可
	vt.resize(max(v1.size(),v2.size()));
	vector<int>::iterator it = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vt.begin());

	cout << "v1和v2的差集:" << endl;
	for_each(vt.begin(), it, myPrint);
	cout << endl;
	vector<int>::iterator it2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vt.begin());
	cout << "v2和v1的差集:" << endl;
	for_each(vt.begin(), it2, myPrint);
	cout << endl;
}

int main()
{
	test01();
	return 0;
}

在这里插入图片描述

✌✌✌

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值