STL中set求交集、并集、差集的方法
并集(http://zh.cppreference.com/w/cpp/algorithm/set_union)
交集(http://zh.cppreference.com/w/cpp/algorithm/set_intersection)
差集(http://zh.cppreference.com/w/cpp/algorithm/set_difference)
inserter(http://zh.cppreference.com/w/cpp/iterator/inserter)
back_inserter(http://zh.cppreference.com/w/cpp/iterator/back_inserter)
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int n;
int num[maxn];
int main(){
set<int> a, b;
vector<int> c;
a = {, , };
b = {,,,,};
//传入的a,b不一定是set, 但一定要有序
set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//并集
for(int n : c) cout << n << " "; puts("");
c.clear(); set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c));//交集
for(int n : c) cout << n << " "; puts("");
c.clear(); set_difference(a.begin(), a.end(), b.begin(), b.end(), back_inserter(c)); //差集(b中属于a的元素去掉)
for(int n : c) cout << n << " "; puts("");
c.clear();
}

STL中set求交集、并集、差集的方法的更多相关文章
- stl set求交集 并集 差集
#include <iostream>#include <set> using namespace std; typedef struct tagStudentInfo{ i ...
- SQL求 交集 并集 差集
故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...
- 如何求ArrayList集合的交集 并集 差集 去重复并集
需要用到List接口中定义的几个方法: addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- (java/javascript) list 交集 并集 差集 去重复并集
java list 交集 并集 差集 去重复并集 package com; import java.util.ArrayList; import java.util.Iterator; import ...
- spark之交集并集差集拉链
spark之交集并集差集拉链 def main(args: Array[String]): Unit = { val sparkConf = new SparkConf().setMaster(&qu ...
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
- Oracle集合运算符 交集 并集 差集
集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集 一.union求并集,公共部分只有包含一次 例:求emp表ename中含’A‘或含有‘M’ SQL&g ...
- java 两个list 交集 并集 差集 去重复并集
前提需要明白List是引用类型,引用类型采用引用传递. 我们经常会遇到一些需求求集合的交集.差集.并集.例如下面两个集合: List<String> list1 = new ArrayLi ...
随机推荐
- hibernate Day1 案例代码
1.创建Person类 package com.icss.pojo; public class Person { private int uid; private String uname; priv ...
- 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
假设你有一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易.注意:你不可同时参与多笔交易(你必须在再次购买前出售掉之前的股票).详见: ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- Suricata的总体架构
Suricata的总体架构 报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...
- ZigBee cc2530芯片学习 error记录(1)
ZigBee cc2530芯片学习 error记录 Error[e46]: Undefined external "LcdInit" referred in main( xxx ...
- 【HEVC帧间预测论文】P1.6 A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence
A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence <HEVC标准介绍.HEVC帧间预测论文笔记&g ...
- qt sql多重条件查询简便方法
转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...
- js由浅入深理解
隐式转换 + - num - 0 把num转换成number: num + "" 把num转换成字符串: ------------------------------------- ...
- 51nod 1067 Bash游戏 V2
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次只能拿1,3,4颗,拿到最后1颗石子的人获胜.假设A B都非常聪 ...
- 关于Ubuntu上的服务文件
问题发现 今天在给ubuntu系统安装ftp服务时,一件奇怪的事引起了我的注意.当我服务安装完成后,想要测试一下是否能控制服务,便输入如下命令: service vsftpd restart 它返回的 ...