In STL, there are algorithms to realize intersection, union, difference and symmetric difference.
Include header file before use:
#include <algorithm>
Note: the algorithm of calculating intersection and union must ensure that the two sets involved in the operation are in order!
Intersection:
For example, find the intersection of {1,2,3} and {2,3,4}:
Functions are required:
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
The first and second parameters are in the form of iterators, which specify the range in which the first set participates in the operation.
The third parameter and the fourth parameter are also in the form of iterators, specifying the scope of the second set to participate in the operation.
The fifth parameter is the insert iterator, which contains two more parameters. The first parameter specifies the set to save the calculation results, and the second parameter is the form of the iterator, which specifies where the calculation results are inserted before the corresponding set.
Later, the functions of calculating Union, difference set and symmetric difference set are the same as those of calculating intersection.
Code:
#include <bits/stdc++.h> #define re register #define e endl using namespace std; const int N = 100005; vector<int> v1, v2, v3; inline int read(){ int f = 0, x = 0; char ch; do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch)); do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch)); return f ? -x : x; } int main(){ for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3 for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4 set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 2 3 for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' '; return 0; }
Union:
Example: find the union of {1,2,3} and {2,3,4}:
Functions are required:
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
Code:
#include <bits/stdc++.h> #define re register #define e endl using namespace std; const int N = 100005; vector<int> v1, v2, v3; inline int read(){ int f = 0, x = 0; char ch; do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch)); do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch)); return f ? -x : x; } int main(){ for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3 for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4 set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 2 3 4 for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' '; return 0; }
Difference sets:
For example, find the difference set of {1,2,3} and {2,3,4}:
Functions are required:
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
Code:
#include <bits/stdc++.h> #define re register #define e endl using namespace std; const int N = 100005; vector<int> v1, v2, v3; inline int read(){ int f = 0, x = 0; char ch; do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch)); do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch)); return f ? -x : x; } int main(){ for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3 for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4 set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' '; return 0; }
Symmetric difference set:
For example, find the symmetric difference set of {1,2,3} and {2,3,4}:
Functions are required:
set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
Code:
#include <bits/stdc++.h> #define re register #define e endl using namespace std; const int N = 100005; vector<int> v1, v2, v3; inline int read(){ int f = 0, x = 0; char ch; do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch)); do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch)); return f ? -x : x; } int main(){ for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3 for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4 set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 4 for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' '; return 0; }