STL 算法vector/set集合-交集,并集,差集,对称差
针对这里提及的四个集合运算必须特别注意:
1、第一个算法需保证第一集合和第二集合有序,并从小到大排序,内部使用默认“<”操作符比较元素大小;
2、第二个算法需保证第一集合和第二集合有序,排序方式参照Compare确定,内部使用Compare比较元素大小。
1 -- set_intersection(交集)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_intersection(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);
该函数用于求两个集合的交集,结果集合中包含所有同时属于第一个集合和第二个集合的元素。例如:集合{1,2,3,7,9}和{3,4,5,7}的交集为{3,7}。
函数返回值:结果集合的结束位置的back_insert_iterator(和普通的迭代器不样)。
参数:
(第一个集合的开始位置,第一个集合的结束位置,第二个参数的开始位置,第二个参数的结束位置,结果集合的插入迭代器(inserter(result, result.begin())))
对于第二个算法,Compare指定用于比较元素大小的仿函数。
2 -- set_union(并集)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_union(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);
该函数用于求两个集合的交集,结果集合中包含所有同时属于第一个集合和第二个集合的元素。例如:集合{1,2,3,7,9}和{3,4,5,7}的并集为{1,2,3,4,5,7}。
3 -- set_difference(差集)
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);
该函数用于求两个集合的差集,结果集合中包含所有属于第一个集合但不属于第二个集合的元素。例如:集合{1,2,3,7,9}和{3,4,5,7}的差集为{1,2,9}。
该函数用于求两个集合的差集,结果集合中包含所有属于第一个集合但不属于第二个集合的元素。例如:集合{1,2,3,7,9}和{3,4,5,7}的差集为{1,2,9}。
4 -- set_symeetric_difference(对称差集)
template<class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,OutputIterator result);template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result, Compare comp);
数学上,两个集合的对称差集是只属于其中一个集合,而不属于另一个集合的元素组成的集合。也就是说对称差集是去除同时出现在两个集合中的元素后,两集合其他元素组成的集合。例如:集合{1,2,3,7,9}和{3,4,5,7}的对称差为{1,2,4,5,9}。集合论中的这个运算相当于布尔逻辑中的异或运算。集合A和B的对称差通常表示为AΔB。
应用举例(以并集为例):
第一类--两个数组求并:
// set_union example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;int main () {int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};vector<int> v; // 也可以用set<int> // 0 0 0 0 0 0 0 0 0 0vector<int>::iterator it;sort (first,first+5); // 5 10 15 20 25sort (second,second+5); // 10 20 30 40 50set_union (first, first+5, second, second+5, inserter(v, v.begin()));// 5 10 15 20 25 30 40 50 0 0for(vector<int>::iterator it=v.begin();it!=v.end();it++)cout<<*it<<" ";return 0;
}
第二类两个set求并:
#include <set>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;int main(void)
{set<int> a,b,c; //c也可以定义成vectora.insert(1);a.insert(6);a.insert(6);b.insert(2);b.insert(6);b.insert(9);//最后一个参数若使用c.begin()会产生编译错误assignment of read-only localtion.set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));for(set<int>::iterator it=c.begin();it!=c.end();it++)cout<<*it<<" ";return 0;
}
第三类--两个vector之间求并:
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;int main()
{vector<int> a,b,c;for(int e=0;e<10;e++){a.push_back(e);b.push_back(e+5);}//最后一个参数若使用c.begin(),运行时会出错“Segmentation fault (core dumped)”.set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c, c.begin()));for(vector<int>::iterator it=c.begin();it!=c.end();it++)cout<<*it<<" ";return 0;
}
注意事项: 函数参数的最后一个参数是插入迭代器,是因为这个函数内部有对结果集合的插入过程,必须用到插入函数,若不需要插入就用普通迭代器即可,就提前就把结果集合的内存空间扩大。
比如:
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;int main()
{vector<int> a,b,c(20); //初始化c中有20个0for(int e=0;e<10;e++){a.push_back(e);b.push_back(e+5);}//最后一个参数若使用c.begin(),运行时会出错“Segmentation fault (core dumped)”.set_union(a.begin(),a.end(),b.begin(),b.end(),c.begin());for(vector<int>::iterator it=c.begin();it!=c.end();it++)cout<<*it<<" ";return 0;
}
若改为c(3)会出现3个元素,最后RE。
此时函数的最后一个参数是普通迭代器,函数的返回值也不再是插入迭代器,也变成了普通迭代器(指向结果集合的最后一个插入元素(非0的))
比如:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;int main () {int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0vector<int>::iterator it;sort (first,first+5); // 5 10 15 20 25sort (second,second+5); // 10 20 30 40 50it=set_union (first, first+5, second, second+5,v.begin());// 5 10 15 20 25 30 40 50 0 0
//如果v.begin()改成inserter(v, v.begin()),函数返回值是插入迭代器,就会出现编译错误,错误信息:
/*E:\Users\amdi\Desktop\zuoye.cpp|165|error: no match for 'operator=' (operand types are 'std::vector<int>::iterator { aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' and 'std::insert_iterator<std::vector<int> >')| */cout << "union has " << int(it - v.begin()) << " elements.\n";return 0;
}
所以求结果集合的元素个数除了用result.size()以外,也可以用以上程序的方法,不过建议还是用插入迭代器,用result.size()毕竟用普通迭代器的问题范围小,而且不方便(我们可能不想去预知结果集合的大小)。
STL 算法vector/set集合-交集,并集,差集,对称差相关推荐
- c++链表实现集合交集并集差集运算
#include<iostream> using namespace std; //创建链表 struct Node { int content; Node* next; ...
- oracle 并集 时间_Oracle集合运算符 交集 并集 差集
集合运算符:UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集 一.union求并集,公共部分只有包含一次 例:求emp表ename中含'A'或含有'M' SQL> ...
- JAVA Hashset求集合的交集并集差集
JAVA Hashset求集合的交集并集差集 Hashset是Set接口较为常见的一个子类,该子类的最大特点是不允许保存重复的元素,并且所有的内容都采用散列(无序)的方式进行存储. package c ...
- A和B 单链表表示 集合,求其 交集 并集 差集
A和B 单链表表示 集合的 交集 并集 差集 /* A和B 单链表表示 集合的 交集 并集 差集 */ //#include <iostream> //using namespace st ...
- java实现交集并集差集
java实现交集并集差集 ***`package 第一次模拟测试; import java.util.ArrayList; import java.util.HashSet; import java. ...
- lambda 对象去重_采用java8 lambda表达式 实现 java list 交集 并集 差集 去重复并集...
采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集 一般的javaList 交.并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的 ...
- 采用java8 lambda表达式 实现 java list 交集 并集 差集 去重复并集
采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集 一般的javaList 交.并集采用简单的 removeAll retainAll 等操作,不过这也破坏了原始的 ...
- go自定义和使用set包处理列表 交集 并集 差集
自定义交集 并集 差集 package mainimport "fmt"func main() {aList := []string{"1", "2& ...
- MySQL求交集 并集 差集
MySQL求交集 并集 差集 交集 两个表之间相同的 并集 两个表之间的总和 ps: union 自带去重 差集 两个表直接的差值
最新文章
- 十进制转化为十六进制分割高低位
- 把握人工智能命脉的有效方法
- C++程序中调用exe可执行文件
- c语言数据域和指针域,C语言的变量域和指针
- 深入浅出WPF笔记——Binding
- 微信小程序的基本结构
- MTK6737 WCN省晶体问题
- Android之高仿记事本、备忘录、便签
- Datewhale组队学习——深度学习推荐系统(1)
- [分享] 揭开美国顶尖生物医学实验室成功的法宝--转
- codeforces202A. Mafia【二分】
- Java实现一个单号生成工具类
- python爬虫----汽车之家的汽车论坛的最新精华帖
- mac os 上启用简单http file server web 文件服务器
- 案例:用逻辑回归制作评分卡
- 高通骁龙845的android手机有哪些,骁龙845手机有哪些?高通骁龙845手机推荐
- oracle co函数,Oracle 常用函数
- 基于Java(SSM框架)实现的购物网站系统【100010082】
- Vip Test 联合创始人陈霁谈 Testops 最优质量反馈
- word2016布局里没有拆分单元格情况下的单元格拆分办法
热门文章
- 东海证券分析报告(0611)
- Nmap扫描原理与用法
- abp angular 和mvc_初识ABP vNext(3):vue对接ABP基本思路
- 开放下载丨悬镜安全携手中国信通院发布《软件供应链安全白皮书(2021)》
- macOS 输入拼音声调
- 【逗老师的小技巧】MacOS重置网络连接的方法
- 零基础小白要如何自学Python?
- 批量将文件名的大写后缀该为小写后缀的代码
- Could NOT find CUDA: Found unsuitable version 9.1, but required is exact version 7.5
- dubbo服务注册流程