C++ 程序以在 STL 中实现 Set_Symmetric_difference
这是一个实现 set_symmetric_difference 的 c++ 程序。两组的对称差可以通过元素构建,这些元素出现在其中一组中,但不出现在另一组中。
常见的集合运算有 −
- 集合并集
- 集合交集
- 对称集合差或异或
- 集合差或减法
算法
Begin Declare set vector v and iterator st. Initialize st = set_symmetric_difference (set1, set1 + n, set2, set2 +n, v.begin())) Print the elements obtained after symmetric_difference of two sets End.
示例代码
#include<iostream> #include <algorithm> #include <vector> using namespace std; int main () { int set1[] = {5,6,7,8,9,10}; int set2[] = {1,2,3,4,6,7}; vector<int> v(10); vector<int>::iterator st; sort (set1, set1 + 6); sort (set2, set2 + 6); st = set_symmetric_difference(set1, set1 + 6, set2, set2 + 6, v.begin()); v.resize(st - v.begin()); cout<<"The symmetric difference between the sets has "<< (v.size())<< " elements: "<<endl; for (st = v.begin(); st != v.end(); ++st) cout<< *st<<" "; cout <<endl; return 0; }
输出
The symmetric difference between the sets has 8 elements: 1 2 3 4 5 8 9 10
广告