|
发表于 2022-4-14 13:31:59
From FishC Mobile
|
显示全部楼层
本楼为最佳答案
执迷不悟0527 发表于 2022-4-14 13:28
好的,如果你有时间能写一下就完美了 ,谢谢你了~
C++- #include <iostream>
- #include <set>
- void print(std::string str, std::set<int> set){
- std::cout << str << ": ";
- for(const int& n: set){
- std::cout
- << n
- << " ";
- }
- std::cout << std::endl;
- }
- int main(){
- std::set<int> x = {3, 12, 5, 6};
- std::set<int> y = {6, 10, 5, 1, 7};
-
- print("x集", x);
- print("y集", y);
-
- std::set<int> a;
- std::set<int> b;
- std::set<int> c1;
- std::set<int> c2;
- std::set<int> d;
-
- std::set_intersection(x.begin(), x.end(), y.begin(), y.end(), inserter(a, a.begin())); // 交集
- std::set_union(x.begin(), x.end(), y.begin(), y.end(), inserter(b, b.begin())); // 合集
- std::set_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(c1, c1.begin())); // 差
- std::set_difference(y.begin(), y.end(), x.begin(), x.end(), inserter(c2, c2.begin())); // 差
- std::set_symmetric_difference(x.begin(), x.end(), y.begin(), y.end(), inserter(d, d.begin())); // 对称差
-
- print("x和y的交集", a);
- print("x和y的合集", b);
- print("x差y的集", c1);
- print("y差x的集", c2);
- print("x和y的对称差集", d);
-
- return 0;
- }
复制代码- x集: 3 5 6 12
- y集: 1 5 6 7 10
- x和y的交集: 5 6
- x和y的合集: 1 3 5 6 7 10 12
- x差y的集: 3 12
- y差x的集: 1 7 10
- x和y的对称差集: 1 3 7 10 12
复制代码 |
|