代码其实很简单,我们只需要知道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类的更多相关文章

  1. spark 集合交集差集运算

    intersect except是spark提供的集合差集运算, 但是要求参与运算的两个dataframe,有相同的data Schema. 如果我想从 集合1(attribute1, attribu ...

  2. EXCEPT差集运算

    EXCEPT差集运算在逻辑上先删除两个输入多集中的重复行,把多集变成集合,然后返回只在第一个集合中出现,在第二个集合中不出现的所有行.可以看下面示意图.

  3. List之Union(),Intersect(),Except() 即并集,交集,差集运算。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. C++求集合的交集差集

    标准库的<algorithm>头文件中提供了std::set_difference,std::set_intersection和std::set_union用来求两个集合的差集,交集和并集 ...

  5. C# 集合的交集 差集 并集 去重

    C# 集合的交集 差集 并集 去重 两个对象list,直接比较是不行的,因为他们存的地址不一样 需要重写GetHashCode()与Equals(object obj)方法告诉电脑 class Stu ...

  6. List使用Stream流进行集合Collection的各种运算汇总:对BigDecimal求和,某个字段的和、最大值、最小值、平均值,字段去重,过滤等

    写Java接口的朋友都知道,Java 8的更新,经常会用到过滤 list<Object> 里的数据,本文就对List使用Stream流进行集合Collection的各种运算做一个汇总! 优 ...

  7. 【数据结构】c语言实现集合的交并差运算

    待改写:存储数据类型int-->char 重复的元素可存储 功能上不完善 #include <stdio.h> #include <stdlib.h> typedef s ...

  8. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  9. Java 数据类型:集合接口Collection之Set接口HashSet类;LinkedHashSet;TreeSet 类

    Collection 之 Set 实现类: HashSet TreeSet 特点: 无序. 元素不可重复. (如果试图添加一个已经有的元素到一个Set集合中,那么会添失败,add()方法返回false ...

随机推荐

  1. Codeforces - 466C - Number of Ways - 组合数学

    https://codeforces.com/problemset/problem/466/C 要把数据分为均等的非空的三组,那么每次确定第二个分割点的时候把(除此之外的)第一个分割点的数目加上就可以 ...

  2. C# 中==和Equal的区别

    http://new-fighter.iteye.com/blog/1634800 今天突然看到一种情况,颠覆了我对这比较使用方法的判断. 于是开始在网上找资料,但几乎都是Java的,好不容易找到一个 ...

  3. P5154 数列游戏(区间dp)

    传送门 果然和dp有关的东西我绝对做不出来啊-- 设\(dp[i][j]\)表示消完区间\([i,j]\)中的数之后能得到的最大分数,如果消不完则为\(-inf\),否则枚举断点.顺便如果\(a[i] ...

  4. (4)javascript的运算符以及运算符的优先级

                                    运算符的使用方法 在javascript的程序中要完成各种各样的运算,是离不开运算符的. 在javascript中,按运算符类型可以分为 ...

  5. 报错org.springframework.dao.DataIntegrityViolationException

    最简单的原因可能是数据库外键字段选择了不能为空, 改为允许为空就行了.

  6. CF580D Kefa and Dishes 【状压dp】By cellur925

    题目传送门 友情链接:new2zydalao%%%  一篇优秀的状压文章 题目大意:$n$个菜有$k$个规则,如果kefa在吃完第$xi$个菜之后吃了第$yi$个菜(保证$xi$.$yi$不相等), ...

  7. Codeforces Round #510 (Div. 2) A&B By cellur925

    第一次CF祭== 由于太菜了只做了前两题== 因为在第一题上耗费时间太多了,我还是太菜了==. A. Benches time limit per test 1 second memory limit ...

  8. USACO Training3.3亚瑟王的宫殿【搜索】By cellur925

    题目传送门 因为太蒟了,所以参考了dalao@zbtrs ==    对此表示感谢并侵删. 看起来我们就知道这是搜索题. 最后的情况分两种:有骑士背国王/国王自食其力走到集合点. 首先,我们不知道大家 ...

  9. Selenium | 基础入门

    在maven项目搭建环境: <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifact ...

  10. 洛谷p2234/BZOJ1588 [HNOI2002]营业额统计

    题目链接: 洛谷 BZOJ 分析: 好像没有什么好说的就是一个平衡树的板子--唯一要注意的就是这里要找的并不是严格的前驱和后继,因为如果找到之前某一天的营业额和它相等那么差就是0,所以我们仍然在结构体 ...