【STL】集合运算
STL中有可以实现交集、并集、差集、对称差集的算法。
使用前需要包含头文件:
#include <algorithm>
注:使用计算交集和并集的算法必须保证参与运算的两个集合有序!!!
交集:
例:求{1,2,3}和{2,3,4}的交集:
需要用到函数:
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
第一个参数和第二个参数是迭代器的形式,指定了第一个集合参与运算的范围。
第三个参数和第四个参数同样是迭代器的形式,指定了第二个集合参与运算的范围。
第五个参数是插入迭代器,它又包含两个参数,第一个参数指定了用来保存计算结果的集合,第二个参数是迭代器的形式,指定了计算结果插入在对应集合的哪个位置之前。
后面计算并集、差集、对称差集的函数与计算交集的函数用法相同,不再赘述。
代码:
#include <bits/stdc++.h>
#define re register
#define e endl
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
int f = 0, x = 0; char ch;
do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
return f ? -x : x;
}
int main(){
for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 2 3
for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
return 0;
}
并集:
例:求{1,2,3}和{2,3,4}的并集:
需要用到函数:
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#include <bits/stdc++.h>
#define re register
#define e endl
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
int f = 0, x = 0; char ch;
do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
return f ? -x : x;
}
int main(){
for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 2 3 4
for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
return 0;
}
差集:
例:求{1,2,3}和{2,3,4}的差集:
需要用到函数:
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#include <bits/stdc++.h>
#define re register
#define e endl
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
int f = 0, x = 0; char ch;
do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
return f ? -x : x;
}
int main(){
for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1
for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
return 0;
}
对称差集:
例:求{1,2,3}和{2,3,4}的对称差集:
需要用到函数:
set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
代码:
#include <bits/stdc++.h>
#define re register
#define e endl
using namespace std;
const int N = 100005;
vector<int> v1, v2, v3;
inline int read(){
int f = 0, x = 0; char ch;
do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
return f ? -x : x;
}
int main(){
for (re int i = 1; i <= 3; ++i)v1.push_back(i);//v1 1 2 3
for (re int i = 2; i <= 4; ++i)v2.push_back(i);//v2 2 3 4
set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));//v3 1 4
for (re int i = 0; i < v3.size(); ++i)cout << v3[i] << ' ';
return 0;
}
【STL】集合运算的更多相关文章
- Problem B: STL——集合运算
Description 集合的运算就是用给定的集合去指定新的集合.设A和B是集合,则它们的并差交补集分别定义如下: A∪B={x|x∈A∨x∈B} A∩B={x|x∈A∧x∈B} A-B={x|x∈A ...
- STL中的set集合容器进行集合运算:并、交、差实例
集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...
- 【Java EE 学习 28 上】【oracle学习第二天】【子查询】【集合运算】【几种数据库对象】
一.子查询 1.为什么要使用子查询:问题不能一步求解或者一个查询不能通过一步查询得到. 2.分类:单行子查询和多行子查询. 3.子查询的本质:一个查询中包含了另外一个或者多个查询. 4.使用子查询的规 ...
- 详解SQL集合运算
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 ...
- SQL集合运算参考及案例(一):列值分组累计求和
概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...
- Oracle学习之集合运算
一.集合运算操作符 UNION:(并集)返回两个集合去掉重复值的所有的记录 UNION ALL:(并集)返回两个集合去掉重复值的所有的记录 INTERSECT:(交集)返回两个集合的所有记录,重复 ...
- Oracle学习(七):集合运算
1.知识点:能够对比以下的录屏进行阅读 SQL> -- 查询10和20号部门的员工的3种方法 SQL> --1. select * from emp where deptno in (10 ...
- sql的集合运算
表的加减法 union:使用union 对表进行假发(并集)运算, union等集合运算符通常都会去除重复记录. select shohin_id, shohin_mei from shohin un ...
- [Swust OJ 632]--集合运算(set容器)
题目链接:http://acm.swust.edu.cn/problem/632/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- SQL集合运算 差集 并集 交
SQL-3标准中提供了三种对检索结果进行集合运算的命令:并集UNION:交集INTERSECT:差集EXCEPT(在Oracle中叫做 MINUS).在有些数据库中对此的支持不够充分,如MySql中只 ...
随机推荐
- 2 DDD理论学习2 领域
一个领域本质上可以理解为就是一个问题域,只要是同一个领域,那问题域就相同. 所以,只要我们确定了系统所属的领域,那这个系统的核心业务,即要解决的关键问题.问题的范围边界就基本确定了. 领域首先要拆分成 ...
- 图像处理结果的度量 —— SNR、PSNR、SSIM
衡量两幅图像的相似度: SNR/PSNR SSIM 1. SNR vs PSNR about SNR 和 PSNR MSE:mean squared error ∑x=1Nx∑y=1Ny(f(x,y) ...
- 简述WPF中的图像像素格式(PixelFormats)
原文:简述WPF中的图像像素格式(PixelFormats) --------------------------------------------------------------------- ...
- Global Contrast based Salient Region Detection (Ming ming Cheng)
abstract: Automatic estimation of salient object regions across images, without any prior assumption ...
- Android 百度定位SDK
原文:Android 百度定位SDK 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/shui1025701856/article/details/7 ...
- the solution about "messy code" in elicpse
I use the Elicpse IDE to develope the ansdroid app.Sometime encounter the messy code in the Elicpse ...
- C++使用libcurl做HttpClient(业务观摩,用C++封装过程式代码,post和get的数据,最好url编码,否则+会变成空格)good
当使用C++做HTTP客户端时,目前通用的做法就是使用libcurl.其官方网站的地址是http://curl.haxx.se/,该网站主要提供了Curl和libcurl.Curl是命令行工具,用于完 ...
- spring boot的默认配置
# BANNER banner.charset=UTF- # Banner file encoding. banner.location=classpath:banner.txt # Banner f ...
- Web应用程序和网站的区别
1项目就是一个应用程序.在VS中查看的时候,项目中建立的一般处理程序,有两个文件,网站只有一个.写个代码测试,发现在代码层次上没有2再有就是项目中的一般处理程序有命名空间,而网站中的没有.WEB网站每 ...
- How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behi ...