离散数学-集合的交并差集运算--STL-set类
代码其实很简单,我们只需要知道set类的使用方法就可以了,比如迭代器的定义( set<T>::iterator it=a.begin() ),和简单的insert函数插入,以及find函数找到时返回对应的迭代器,找不到时返回该set的end。
#include <iostream>
#include <set>
#define T int
using namespace std;
void ShowSet(set<T> V)
{
set<T>::iterator it=V.begin();
for (;it!=V.end();it++) {
cout<<*it<<" ";
}
cout<<endl;
}
void inte(set<T>a,set<T>b)
{
set<T>::iterator it=a.begin();
for (;it!=a.end();it++) {
if (b.find(*it)!=b.end()) {
cout<<*it<<" ";
}
}
cout<<endl;
}
void Sub(set<T>a,set<T>b)
{
set<T>::iterator it=a.begin();
for (;it!=a.end();it++) {
if (b.find(*it)!=b.end())
continue;
cout<<*it<<" ";
}
cout<<endl;
}
void unio(set<T>a,set<T>b,set<T>c)
{
set<T>::iterator it=a.begin();
for (;it!=a.end();it++) {
c.insert(*it);
}
it=b.begin();
for (;it!=b.end();it++) {
c.insert(*it);
}
ShowSet(c);
}
int main()
{
set<T> a,b,c;
T em;
// while (cin>>ch&&ch!='0')
// {
// a.insert(ch-'0');
// }
// ShowSet(a);
int num1,num2;
cout<<"Please enter the number of the sets A and B."<<endl;
cin>>num1>>num2;
while (num1--) {
cin>>em;
a.insert(em);
}
while (num2--) {
cin>>em;
b.insert(em);
}
cout<<"Please enter the operation to be performed.I stands for intersection."<<endl
<<"U stands for union.S is the different set."<<endl;
char ch;
while (cin>>ch) {
if (ch=='I') {
inte(a,b);
}
else if (ch=='U') {
unio(a,b,c);
}
else if (ch=='S') {
bool is;
cout<<"Which one do you want to execute? A - B is true , B - A is false."<<endl;
cin>>is;
if (is==1)
Sub(a,b);
else
Sub(b,a);
}
else
cout<<"Input error!"<<endl;
}
return 0;
}
/*测试数据:
5 5
1 2 3 4 5
1 2 7 8 9
I
U
S
1
S
0
*/
离散数学-集合的交并差集运算--STL-set类的更多相关文章
- spark 集合交集差集运算
intersect except是spark提供的集合差集运算, 但是要求参与运算的两个dataframe,有相同的data Schema. 如果我想从 集合1(attribute1, attribu ...
- EXCEPT差集运算
EXCEPT差集运算在逻辑上先删除两个输入多集中的重复行,把多集变成集合,然后返回只在第一个集合中出现,在第二个集合中不出现的所有行.可以看下面示意图.
- List之Union(),Intersect(),Except() 即并集,交集,差集运算。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C++求集合的交集差集
标准库的<algorithm>头文件中提供了std::set_difference,std::set_intersection和std::set_union用来求两个集合的差集,交集和并集 ...
- C# 集合的交集 差集 并集 去重
C# 集合的交集 差集 并集 去重 两个对象list,直接比较是不行的,因为他们存的地址不一样 需要重写GetHashCode()与Equals(object obj)方法告诉电脑 class Stu ...
- List使用Stream流进行集合Collection的各种运算汇总:对BigDecimal求和,某个字段的和、最大值、最小值、平均值,字段去重,过滤等
写Java接口的朋友都知道,Java 8的更新,经常会用到过滤 list<Object> 里的数据,本文就对List使用Stream流进行集合Collection的各种运算做一个汇总! 优 ...
- 【数据结构】c语言实现集合的交并差运算
待改写:存储数据类型int-->char 重复的元素可存储 功能上不完善 #include <stdio.h> #include <stdlib.h> typedef s ...
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- Java 数据类型:集合接口Collection之Set接口HashSet类;LinkedHashSet;TreeSet 类
Collection 之 Set 实现类: HashSet TreeSet 特点: 无序. 元素不可重复. (如果试图添加一个已经有的元素到一个Set集合中,那么会添失败,add()方法返回false ...
随机推荐
- HDU6024:Building Shops(DP)
传送门 题意 在一条直线上有n个教室,现在要设置糖果店,使得最后成本最小,满足以下两个条件: 1.若该点为糖果店,费用为cost[i]; 2.若不是,则为loc[i]-最近的糖果店的loc 分析 dp ...
- bzoj 4550: 小奇的博弈【博弈论+dp】
首先看出终止状态是全都堆在左边或者右边,然后发现黑的向左白的向右是最优策略(如果不能这样了也就该输了) 然后就不会了 参考 http://www.cnblogs.com/CQzhangyu/p/770 ...
- Springboot 配置 application.yml 连接MySQL数据库
1.在pom.xml的<dependencies></dependencies>标签中中加入以下依赖 <dependency> <groupId>org ...
- 更换过Ubuntu之后经常性卡死,原因有待细究
如题: 卡死时间: 2019-5-22-14:45 再次卡死,这次绝对不是看视频的原因了,具体什么原因还是不知道,不过我觉得就是显卡的问题,和搜索出来的问题差不多,安装了一些东西,看看行不行吧 sud ...
- Nginx系列篇二:linux搭建Nginx负载均衡
建议先搭建好Nginx环境 可阅读--->Linux中搭建Nginx 1.准备好三台服务器[标配] 一.nginx负载均衡服务器:192.168.102.110,配置好Nginx 二.tomca ...
- A Dangerous Maze LightOJ - 1027
这题意真是... 题意:你在一个迷宫里,有一些门,每个门有一个参数x,如果为正表明你进入门后可以花x的时间出去,如果为负表明你进入门后可以花-x的时间回到出发的地方.每次回到出发的地方之后,不能记得之 ...
- 求n的因子个数与其因子数之和
方法一:朴素算法:O(n). #include<bits/stdc++.h> using namespace std; int get_num(int n){ ; ;i<=n;++i ...
- Nagios安装与部署
Nagios概述: Nagios是一款开源免费(也有收费版的Nagios XI)的监控工具,可以用以监控Windows.Linux.Unix.Router.Switch,可以监控指定主机的物理基础资源 ...
- rhel 6.5--samba
配置匿名共享: 服务端: [root@master ~]# yum install -y samba 或者 [root@master ~]# yum groupinstall -y "CIF ...
- AJPFX简述Java中this关键字的使用
Java中this关键字的使用主要有两处: 1.构造方法 this指的是调用构造方法进行初始化的对象. //有参构造public Human(String name, int age) { this( ...