set_union set_intersection
来源:互联网 发布:jquery rotate.js cdn 编辑:程序博客网 时间:2024/05/16 17:54
#include <iostream> // std::cout
#include <algorithm> // std::set_union, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it;
std::sort (first,first+5); // 5 10 15 20 25
std::sort (second,second+5); // 10 20 30 40 50
it=std::set_union (first, first+5, second, second+5, v.begin());
// 5 10 15 20 25 30 40 50 0 0 //参数:求并集的两个集合(数组或者set等其他类型)的起止地址,最后一个参数是前两个集合并集的结果需要插入的地方
v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50
std::cout << "The union has " << (v.size()) <<" elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
output
The union has 8 elements: 5 10 15 20 25 30 40 50
以上来自http://www.cplusplus.com/reference/algorithm/set_union/?kw=set_union;
set_union可以对任意类型起作用。
返回值是最后一个插入的位置。
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int>x1;
x1.insert(1);
x1.insert(2);
x1.insert(3);
set<int>x2;
x2.insert(1);
x2.insert(4);
set<int>x;
set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
for(set<int>::iterator it=x.begin();it!=x.end();it++)
cout<<*it<<' ';
cout<<endl;
}
输出:1 2 3 4
set_intersection:(求交集可用)
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int>x1;
x1.insert(1);
x1.insert(2);
x1.insert(3);
set<int>x2;
x2.insert(1);
x2.insert(4);
set<int>x;
set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
for(set<int>::iterator it=x.begin();it!=x.end();it++)
cout<<*it<<' ';
cout<<endl;
}
输出:1
- set_union set_intersection
- set_union,set_intersection,set_difference,set_symmetric_difference
- C++ set_union,set_intersection,set_difference
- STL set_difference set_intersection set_union 操作
- C++ STL merge,set_union,set_intersection,set_difference,set_symmetric_difference
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- STL之set_union、set_intersection、set_difference、set_symmetric_difference
- vector set_union() /set_intersection【集合合并/交集】
- 泛型算法系列35:set_difference()&&set_intersection()&&set_symmetric_difference()&&set_union()
- 泛型算法:有序集合set_union和set_intersection
- C++集合运算函数总结-merge、set_union、set_difference、set_symmetric_difference、set_intersection
- set_intersection
- set_intersection
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- set_union
- Java web 环境搭建 环境依赖 操作系统 :Windows 7 开发工具 :IntelliJ IDEA 开发工具依赖环境
- linux内核线程 [创建]
- FunDA(6)- Reactive Streams:Play with Iteratees、Enumerator and Enumeratees_1
- VerilogHDL语言实现组合逻辑电路
- Java httpclient请求
- set_union set_intersection
- C12-1 等腰三角形的面积
- 数据流重导向
- ChucK初步(4)
- 如果你不是每天被人拒绝,那就说明你的人生目标不够远大。(If you aren't getting rejected on a daily basis, your goals aren't ambit
- suse linux 文件目录备份和恢复
- git最常用命令
- Leetcode 115 - Distinct Subsequences(dp)
- android便于维护的下面选项卡状态切换