0.前言
1.set_intersection
#include <iostream>
using namespace std;// 常用集合算法 交集set_intersection
#include<vector>
#include<algorithm>void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1, v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>TargetV;TargetV.resize(min(v1.size(), v2.size()));vector<int>::iterator Itend = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), TargetV.begin());for_each(TargetV.begin(), Itend, myPrint);cout << endl;
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
2.set_union
#include <iostream>
using namespace std;// 常用集合算法 并集set_union
#include<vector>
#include<algorithm>void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1, v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>TargetV;//目标容器提前开辟空间//最特殊情况 两个容器没有交集, 并集就是两个容器size相加TargetV.resize(v1.size() + v2.size());vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), TargetV.begin());for_each(TargetV.begin(), itEnd, myPrint);cout << endl;
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}
3.set_difference
#include <iostream>
using namespace std;// 常用集合算法 差集set_difference
#include<vector>
#include<algorithm>void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1, v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>TargetV;//给目标容器开辟空间//最特殊情况 两个容器没有交集 取两个容器中大的size作为目标容器开辟空间TargetV.resize(max(v1.size(), v2.size()));vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), TargetV.begin());cout << "v2和v1的差集:" << endl;set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), TargetV.begin());for_each(TargetV.begin(), itEnd, myPrint);cout << endl;cout << "v1和v2的差集:" << endl;set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), TargetV.begin());for_each(TargetV.begin(), itEnd, myPrint);cout << endl;
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
}