离散数学-集合的交并差集运算--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 ...
随机推荐
- bzoj 4199: [Noi2015]品酒大会【后缀数组+单调栈+并查集】
用SA求出height数组,然后发现每个height值都有一个贡献区间(因为点对之间要依次取min) 用单调栈处理出区间,第一问就做完了 然后用并查集维护每个点的贡献(?),从大到小枚举height, ...
- 五个demo案例带你学习PHP反序列化漏洞
一直想研究下php反序列化漏洞,花了几天时间做了个简单的了解..写篇文章记录下. 直白点就是围绕着serialize和unserialize两个函数. 一个用于序列化,一个用于反序列化. 我们通常把字 ...
- AFNetworking https自签名证书 -1012 解决方案
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy]; //是否信任服务器无效或过期的SSL证书.默认为“不”. se ...
- 继续(3n+1)猜想 (25)
#include <algorithm> #include <iostream> using namespace std; int main(){ ] = { }; ], nu ...
- .Net开发人员必备工具下载
.Net开发人员必备工具下载 本人亲测下载地址: Win8.1破解工具下载: http://pan.baidu.com/s/1eQf2UiQ 可激活版本 Windows Vista Busines ...
- Python while 1 和 while True 速度比较
References http://legacy.python.org/dev/peps/pep-0285/http://stackoverflow.com/questions/3815359/whi ...
- Milking Grid POJ - 2185 || 最小覆盖子串
Milking Grid POJ - 2185 最小覆盖子串: 最小覆盖子串(串尾多一小段时,用前缀覆盖)长度为n-next[n](n-pre[n]),n为串长. 当n%(n-next[n])==0时 ...
- double发生精度丢失的解决办法
发生精度丢失的原因: 个人理解:机器在运行时,使用2进制形式的计数方式,而我们日常生活中的计算是10进制的,对于整数的加减乘除,double还能适用,但是对于有小数的,则容易发生精度丢失,即用2进制表 ...
- P1554 梦中的统计
题目背景 Bessie 处于半梦半醒的状态.过了一会儿,她意识到她在数数,不能入睡. 题目描述 Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数.她开始注意每一个数码(0..9):每一 ...
- 序列化json模块
1.用json模块来进行序列化和反序列化 注意:用json序列化的数据类型得到的文件后缀名必须是json.因为如果不是json后缀,别人也不知道这是用json序列化的文件. 序列化:json.dump ...