C++ 程序在 STL 中实现 Set_Union


两个集合的并集是存在于任一集合或同时存在于两个集合中的元素创建的。第二个集合中与第一个集合中具有相同元素的元素不会被复制到结果集合中。

常见的集合操作有 -

  • 集合并集
  • 集合交集
  • 对称集合差异或异或
  • 集合差异或减法

算法

Begin
   Declare set vector v and iterator st.
   Initialize st= set_union (set1, set1 + n, set2, set2 +n, v.begin()))
   Print the elements.
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_union(set1, set1 + 6, set2, set2 + 6, v.begin());
   v.resize(st - v.begin());
   cout<<"The union between the sets has "<< (v.size())<< " elements: "<<endl;
   for (st = v.begin(); st != v.end(); ++st)
      cout<< *st<<" ";
   cout <<endl;
   return 0;
}

输出

The union between the sets has 10 elements:
1 2 3 4 5 6 7 8 9 10

更新日期: 30-7-2019

588 次浏览

启动您的 职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.