STL中有可以实现交集、并集、差集、对称差集的算法。
使用前需要包含头文件:
#include <algorithm>
注:使用计算交集和并集的算法必须保证参与运算的两个集合有序!!!
交集:
例:求{1,2,3}和{2,3,4}的交集:
需要用到函数:
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
第一个参数和第二个参数是迭代器的形式,指定了第一个集合参与运算的范围。
第三个参数和第四个参数同样是迭代器的形式,指定了第二个集合参与运算的范围。
第五个参数是插入迭代器,它又包含两个参数,第一个参数指定了用来保存计算结果的集合,第二个参数是迭代器的形式,指定了计算结果插入在对应集合的哪个位置之前。
后面计算并集、差集、对称差集的函数与计算交集的函数用法相同,不再赘述。
代码:
#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; }
并集:
例:求{1,2,3}和{2,3,4}的并集:
需要用到函数:
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#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; }
差集:
例:求{1,2,3}和{2,3,4}的差集:
需要用到函数:
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#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; }
对称差集:
例:求{1,2,3}和{2,3,4}的对称差集:
需要用到函数:
set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#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; }