离散数学-集合的交并差集运算--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 1090: [SCOI2003]字符串折叠【区间dp】
设f[i][j]为区间(i,j)的最短长度,然后转移的话一个是f[i][j]=min(j-i+1,f[i][k]+f[k+1][j]),还有就是把(k+1,j)合并到(i,k)上,需要判断一下字符串相 ...
- fiddler安装及抓取http和https请求
安装fiddler 安装完成,此时就可以抓取http请求了 如果要抓取https请求,就需要更新fiddler为最新版,并安装证书 1.检查更新fiddler为最新版 2.下载证书并安装 https证 ...
- Android SDK Manager 无法下载Android8.1.0(API 27) SDK Platform
在Android SDK Manager 中安装Android 8.1.0 SDK Platform时报错导致无法安装. 错误信息:Downloading SDK Platform Android 8 ...
- Qt文本读写之二:目录操作
一.简介 QDir类用来访问目录结构及其内容,可以操作路径名.访问路径和文件相关信息以及操作底层的文件系统,还可以访问Qt的资源系统.Qt使用"/"作为通用的目录分隔符和URLs的 ...
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
- Jquary基础
基本知识: 就是一个JS函数包 选择器:基本选择器: 基本:ID选择器 “#” , Class选择器 “.”,标签选择器 “标签名” 组合:并列用“,”隔开 后代用空格隔开 过滤选择器:基本过滤: ...
- 通用mapper的generator
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-genera ...
- Backbone.js之Todo源码浅析
相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...
- k-window的关闭与打开设置
// 打开弹框窗口 public showKwinDow() { const that = this as any; // 设置窗口居中 that.$refs['setAddEdit'].widget ...
- 在git远程仓创建项目之后,提交本地项目的使用方法
命令介绍 git 用户配置 git config --global user.name "张三" git config --global user.email "zhag ...