STL.set Union, Intersection, Difference and Symmetric Difference

STL provides a total of four set-related algorithms, namely union, intersection, difference, and symmetric difference.
The set accepted by these four algorithms of STL must be an ordered interval, and the elements can appear repeatedly. That is, they can only accept set/multiset containers as input ranges.
1. The set_unoin (union set)
algorithm set_union can construct the union of two sets of S1 and S2. This union contains all the elements in the two sets of S1 and S2. The return value of set_union is an iterator pointing to the end of the output range. Since the elements in S1 and S2 in the multiset do not need to be unique, if a value appears n times in S1 and m times in S2, the value will appear max(m,n) times in the union.

write picture description here

2. set_intersection (seeking intersection)
algorithm set_intersection is an algorithm that can construct the intersection of two sets S1 and S2. This intersection contains elements common to S1 and S2. Since elements in a multiset can appear repeatedly, if a value appears n times in S1 and m times in S2, it will appear min(n,m) times in the intersection.

write picture description here

3. The set_difference
algorithm set_difference can construct the difference set of S1 and S2. It can construct S1-S2, which means that S1 appears but does not appear in each element of S2. Since the values ​​of elements in a multiset may repeat, if a value occurs n times in S1 and m times in S2, then the value will appear max(nm, 0) times in the difference set, and all come from S1.

write picture description here


4. The set_symmetric_difference (seeking symmetric difference set)
algorithm set_symmetric_difference can construct a symmetric difference set of S1 and S2 sets. The so-called symmetric difference set is (S1-S2)U(S2-S1), which means that it appears in the S1 set but not in the set S2 set appears, the set of elements that appear in S2 set but not in S1 set. Since elements in a multiset can be repeated, if a value appears n times in S1 and m times in S2, then the value appears |nm| times in the symmetric difference set.

write picture description here

Implementation code:

#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("Set1 : ", Set1);
	DispSet("Set2: ", Set2);

	//intersection
	vector<int> vUnionSet; vUnionSet.resize(Set1.size() + Set2.size());
	DispVector("Intersection of set 1 and 2: ", vUnionSet.begin(), set_union(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vUnionSet.begin()) );

	//union
	vector<int> vIntersectionSet; vIntersectionSet.resize((Set1.size() > Set2.size() ? Set2.size() : Set1.size()));
	DispVector("Union of Sets 1 and 2: ", vIntersectionSet.begin(), set_intersection(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vIntersectionSet.begin() ));

	// difference
	vector<int> vDifferenceSet1; vDifferenceSet1.resize(Set1.size());
	DispVector("The difference between set 1 and set 2: ", vDifferenceSet1.begin(), set_difference(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vDifferenceSet1.begin ()));

	// difference
	vector<int> vDifferenceSet2; vDifferenceSet2.resize(Set2.size());
	DispVector("The difference between set 2 and set 1: ", vDifferenceSet2.begin(), set_difference(Set2.begin(), Set2.end(), Set1.begin(), Set1.end(), vDifferenceSet2.begin ()));

	//symmetric difference
	vector<int> vSymmetricDifferenceSet; vSymmetricDifferenceSet.resize(Set1.size() + Set2.size());
	DispVector("The symmetric difference of sets 1 and 2: ", vSymmetricDifferenceSet.begin(), set_symmetric_difference(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), vSymmetricDifferenceSet.begin( )));
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762565&siteId=291194637