关于C++中使用set_union、set_intersection、set_difference的总结
2018-03-01 18:20
549 查看
看紫书例题5-5时,总会遇到不知道怎么用的函数,每次都会进行查找理解,这次参考了http://blog.csdn.net/zangker/article/details/22984803的博客...
STL set中有set_union(取两集合并集)、set_intersection(取两集合交集)、set_difference(取两集合差集)。
1、这几个函数的参数一样。2、set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.end())),前两个参数是集合x1的头尾,再依次是集合x2的头尾,最后一个参数就是将集合x1和集合x2取合集后存入集合x中。
输出结果:
STL set中有set_union(取两集合并集)、set_intersection(取两集合交集)、set_difference(取两集合差集)。
1、这几个函数的参数一样。2、set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.end())),前两个参数是集合x1的头尾,再依次是集合x2的头尾,最后一个参数就是将集合x1和集合x2取合集后存入集合x中。
/* Input: 第一行输入一个正整数T,表示测试次数; 然后下面有2T行,每一行都有n+1个数字,其中第一个数字是n(0<=n<=100),表示该行后面还有n个数字输入。 Output: 对于每组测试数据,首先输出测试数据序号,”Case #.NO”, 接下来输出共5行,每行都是一个集合, 前2行分别输出集合A、B,接下3行来分别输出集合A、B的并(A u B)、交(A n B)、差(A – B)。 集合中的元素用“{}”扩起来,且元素之间用“,”隔开。 Input: 1 1 2 3 1 0 Output: Case# 1: A = {1, 2, 3} B = {} A u B = {1, 2, 3} A n B = {} A - B = {1, 2, 3} */ #include<iostream> #include<set> #include<cstdio> #include<algorithm> using namespace std; int main() { set<int> x1; set<int> x2; set<int> C1; set<int> C2; set<int> C3; set<int>::iterator pos; int T,flag=0; cin >> T; while(T--) { flag++; int x1_i,x2_i,item; cin>>x1_i; for(int i = 0; i < x1_i; i++) { cin >> item; x1.insert(item); } cin >> x2_i; for(int i = 0; i < x2_i; i++) { cin >> item; x2.insert(item); } cout << "Case: " << flag <<endl; cout << "x1 = { "; for(pos = x1.begin(); pos != x1.end(); pos++) { if(pos != x1.begin()) cout << " ,"; cout << *pos; } cout << " }" << endl << "x2 = { "; for(pos = x2.begin(); pos != x2.end(); pos++) { if(pos != x2.begin()) cout << " ,"; cout << *pos; } cout<< " }" << endl << "x1 u x2 = { "; set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(C1, C1.begin())); for(pos = C1.begin(); pos != C1.end(); pos++) { if(pos != C1.begin()) cout << " ,"; cout << *pos; } cout<< " }" << endl << "x1 n x2 = { "; set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(C2, C2.begin())); for(pos = C2.begin(); pos != C2.end(); pos++) { if(pos != C2.begin()) cout << " ,"; cout << *pos; } cout<< " }" << endl << "x1 - x2 = {"; set_difference(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(C3, C3.begin())); for(pos = C3.begin(); pos != C3.end(); pos++) { if(pos != C3.begin()) cout << " ,"; cout << *pos; } cout << " }" << endl; } return 0; }
输出结果:
相关文章推荐
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- C++ STL merge,set_union,set_intersection,set_difference,set_symmetric_difference
- C++集合运算函数总结-merge、set_union、set_difference、set_symmetric_difference、set_intersection
- 总结C++中三种关于"new"的使用方法
- 总结C++中三种关于"new"的使用方法
- c++高级---关于C++指针大小和Void*使用的总结
- C++ union 使用总结
- C++中关于sizeof使用的总结
- (转帖)总结C++中三种关于"new"的使用方法
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- C++ STL 有关于SET集合部分 的自定义数据类型的排序 以及 pair的使用
- 关于C++中vector和set使用sort方法进行排序
- 总结C++中三种关于"new"的使用方法
- C++ set_union,set_intersection,set_difference
- C++ union 使用总结
- 总结C++中三种关于”new”的使用方法
- 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation
- 几个STL算法:includes,set_difference、set_intersection、set_symmetric_difference、set_union, pre_permutation, next_permutation