STL一共提供了四种set相关的算法,分别是并集(union),交集(intersection),差集(difference),和对称差集(symmetric difference)。
STL的这四个算法所接受的set必须是有序区间,元素可以重复出现。即他们只能接受set/multiset容器作为输入区间。
1、set_unoin(求并集)
算法set_union可构造两个S1,S2这两个集合的并集,这个并集里面包含了S1和S2这两个集合里面的所有元素。set_union的返回值是一个迭代器,指向输出区间的尾端。由于multiset中S1和S2内的元素不需要唯一,所以如果某个值在S1出现n次,在S2出现m次,则这个值会在并集中出现max(m,n)次。
STL的这四个算法所接受的set必须是有序区间,元素可以重复出现。即他们只能接受set/multiset容器作为输入区间。
1、set_unoin(求并集)
算法set_union可构造两个S1,S2这两个集合的并集,这个并集里面包含了S1和S2这两个集合里面的所有元素。set_union的返回值是一个迭代器,指向输出区间的尾端。由于multiset中S1和S2内的元素不需要唯一,所以如果某个值在S1出现n次,在S2出现m次,则这个值会在并集中出现max(m,n)次。
2、set_intersection(求交集)
算法set_intersection是一种可以构造两个集合S1,S2交集的算法,这个交集里面包含了S1和S2共有的元素。由于multiset中元素可以重复出现,因此如果某个值在S1出现n次,在S2出现m次,则在交集中出现min(n,m)次。
3、set_difference(求差集)
算法set_difference可以构造S1和S2的差集,他能构造出S1-S2,表示出现S1但不出现于S2的每一个元素。由于multiset中元素的值可能重复,因此如果某个值在S1中出现n次,在S2中出现m次,那么这个值在差集中会出现max(n-m,0)次,并且全部来自S1。
4、set_symmetric_difference(求对称差集)
算法set_symmetric_difference可构造S1和S2集合的对称差集,所谓的对称差集就是说(S1-S2)U(S2-S1),意思就是说在S1集合出现但不在S2集合出现,在S2集合出现但没在S1集合出现的元素所组成的集合。由于multiset中元素可以重复出现,因此如果某个值在S1中出现n次,在S2中出现m次,那么该值在对称差集中出现|n-m|次。
实现代码:
#include<iostream> #include<iomanip> #include<string> #include<cstring> #include<algorithm> #include<set> #include<iterator> #include<vector> using namespace std; void DispSet(string strSet, set<int>& Set){ cout << strSet << endl << "*********************************************************" << endl; transform(strSet.begin(), strSet.end(), strSet.begin(), ::toupper); for(set<int>::iterator it = Set.begin(); it != Set.end(); it++){ cout << setw(3) << (*it); } cout << endl << endl << endl; } void DispVector(string strVec, vector<int>::iterator& itBegin, vector<int>::iterator& itEnd){ cout << strVec << endl << "*********************************************************" << endl; for(vector<int>::iterator it = itBegin; it != itEnd; it++){ cout << setw(3) << (*it); } cout << endl << endl << endl; } void UnionSet() { int Array1[5] = {1, 3, 4, 5, 6}; int Array2[5] = {0, 1, 2, 5, 7}; set<int> Set1(Array1, Array1+5); set<int> Set2(Array2, Array2+5); DispSet("集合1 : ", Set1); DispSet("集合2 : ", Set2); //交集 vector<int> vUnionSet; vUnionSet.resize(Set1.size() + Set2.size()); DispVector("集合1、2的交集 : ", vUnionSet.begin(), set_union(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vUnionSet.begin())); //并集 vector<int> vIntersectionSet; vIntersectionSet.resize((Set1.size() > Set2.size() ? Set2.size() : Set1.size())); DispVector("集合1、2的并集 : ", vIntersectionSet.begin(), set_intersection(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vIntersectionSet.begin())); //差集 vector<int> vDifferenceSet1; vDifferenceSet1.resize(Set1.size()); DispVector("集合1相对于集合2的差集 : ", vDifferenceSet1.begin(), set_difference(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vDifferenceSet1.begin())); //差集 vector<int> vDifferenceSet2; vDifferenceSet2.resize(Set2.size()); DispVector("集合2相对于集合1的差集 : ", vDifferenceSet2.begin(), set_difference(Set2.begin(), Set2.end(), Set1.begin(), Set1.end(), vDifferenceSet2.begin())); //对称差集 vector<int> vSymmetricDifferenceSet; vSymmetricDifferenceSet.resize(Set1.size() + Set2.size()); DispVector("集合1、2的对称差集 : ", vSymmetricDifferenceSet.begin(), set_symmetric_difference(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vSymmetricDifferenceSet.begin())); }