#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<numeric>//算术生成算法头文件
#include<string>
#include<ctime>
using namespace std;set_intersection 是 C++ 中的一个算法,用于计算两个有序集合的交集,并将结果存储在另一个集合中。
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first); 
在上述函数签名中,first1 和 last1 表示第一个有序集合的开始和结束迭代器,first2 和 last2 表示第二个有序集合的开始和结束迭代器。d_first 是输出结果的起始位置迭代器。
//两个集合必须有序
struct myfunc2
{bool operator()(int v1, int v2){return v1 > v2;}
};
void test04()
{vector<int>v1;/*for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;for (int i = 4; i < 15; i++){v2.push_back(i);}*/for (int i = 10; i >=0; i--){v1.push_back(i);}vector<int>v2;for (int i = 15; i >4; i--){v2.push_back(i);}vector<int>v3;v3.resize(min(v1.size(), v2.size()));set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),myfunc2());for_each(v3.begin(), v3.end(), [](int val)->void {cout << val << " "; });}set_union 是 C++ 中的一个算法,用于计算两个有序集合的并集,并将结果存储在另一个集合中。
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first); 
在上述函数签名中,first1 和 last1 表示第一个有序集合的开始和结束迭代器,first2 和 last2 表示第二个有序集合的开始和结束迭代器。d_first 是输出结果的起始位置迭代器。
//set_union算法,求并集
void test05()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;for (int i = 4; i < 15; i++){v2.push_back(i);}vector<int>v3;v3.resize(v1.size() + v2.size());set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), v3.end(), [](int val)->void {cout << val << " "; });
}
set_difference 是 C++ 中的一个算法,用于计算两个有序集合的差集,并将结果存储在另一个集合中。
template<class InputIt1, class InputIt2, class OutputIt> OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first); 
在上述函数签名中,first1 和 last1 表示第一个有序集合的开始和结束迭代器,first2 和 last2 表示第二个有序集合的开始和结束迭代器。d_first 是输出结果的起始位置迭代器。
//set_difference算法,求两个set集合的差集
//A(1,2,3,4,5)  B(2,3,4,5,6)A减B=1;B-A=6
void  test06()
{vector<int>v1 = { 1,2,3,4,5 };vector<int>v2 = { 3,4,5,6 };vector<int>v3;v3.resize(min(v1.size(), v2.size()));set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), v3.end(), [](int val)->void {cout << val << " "; });
}