离散数学-集合的交并差集运算--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 ...
随机推荐
- 一个表的两个字段具有相同的类型。如何仅用SQL语句交换这两列的数据?
--假设为A B两个字段--查询Select A As B, B As A From TableName --更新Update TableName Set A = B, B = A
- 51nod1256【exgcd求逆元】
思路: 把k*M%N=1可以写成一个不定方程,(k*M)%N=(N*x+1)%N,那么就是求k*M-N*x=1,k最小,不定方程我们可以直接利用exgcd,中间还搞错了: //小小地讲一下exgcd球 ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- bzoj 3594: [Scoi2014]方伯伯的玉米田【二维树状数组+dp】
设f[i][j]为前i棵玉米被拔高了j(因为是单调不降所以前面越高越好,所以每次拔一个前缀),转移是f[i][j]=f[k][l]+1,l<=j,a[k]+l<=a[i]+j,然后用二维树 ...
- boost 编译 asio 程序,简单socket 编程
自己第一次玩boost,对C++也非常不熟悉,记录一下自己的学习过程. 安装编译 boost 包解压到/opt下 tar -zxvf /media/C06EDE596EDE47B4/mnt/boost ...
- Java中JRE、JDK和JVM的区别
一.三者的基本概念: JRE(Java Development Kit):Java的运行环境: JDK(Java Runtime Enviroment):Java开发工具包: JVM(Java Vir ...
- Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row 1
前端插入数据的时候报如下错误: Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row ...
- 大数模板 (C ++)
上次BC遇到一个大数题目,没有大数模板和不会使用JAVA的同学们GG了,赛后从队友哪里骗出大数模板.2333333,真的炒鸡nice(就是有点长),贴出来分享一下好辣. //可以处理字符串前导零 #i ...
- Android课程设计第六天欢迎界面(跳转)
注意:课程设计只为完成任务,不做细节描述~ package com.example.myapplication; import android.app.Activity; import android ...
- AIDL(1):简介
Android 接口定义语言 (AIDL) 1.AIDL是什么 AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似. 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进 ...