STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
includes,set_intersection,set_union,set_difference,set_symmetric_difference都是集合论中的操作。这些算法都假定指定区间类的元素已经有序。
includes:A∈B
set_intersection:A∩B
set_union:A∪B
set_difference:A-B
set_symmetric_difference:(A-B)∪(B-A)
1,includes
声明:
- #include <algorithm>
- template <class inputItr1, class inputItr2>
- bool includes(inputItr1 first1, inputItr1 last1, inputItr2 first2, inputItr2 last2);
- template <class inputItr1, class inputItr2, class binaryPredicate>
- bool includes(inputItr1 first1, inputItr1 last1, inputItr2,binaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- char setA[5] = {'A','B','C','D','E'};
- char setB[10] = {'A','B','C','D','E','F','I','J','K','L'};
- char setC[5] = {'A','E','I','O','U'};
- ostream_iterator<char> screen(cout, " ");
- cout << "setA:" << endl;
- copy(setA,setA+5,screen);
- cout << endl;
- cout << "setB:" << endl;
- copy(setB,setB+10,screen);
- cout << endl;
- cout << "setC:" << endl;
- copy(setC,setC+5,screen);
- cout << endl;
- if (includes(setB,setB+10,setA,setA+5))
- {
- cout << "setA is a subset of setB" << endl;
- } else {
- cout << "setA is not a subset of setB" << endl;
- }
- if (includes(setB,setB+10,setC,setC+5))
- {
- cout << "setC is a subset of setB" << endl;
- } else {
- cout << "setC is not a subset of setB" << endl;
- }
- return 0;
- }
运行结果:
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
2,set_intersection,set_union
声明:
- #include <algorithm>
- template <class inputItr1, class inputItr2,class outputItr>
- outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
- template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
- outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
- template <class inputItr1, class inputItr2,class outputItr>
- outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
- template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
- outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- int setA[5] = {2,4,5,7,8};
- int setB[7] = {1,2,3,4,5,6,7};
- int setC[5] = {2,5,8,8,15};
- int setD[6] = {1,4,4,6,7,12};
- int AunionB[10];
- int AunionC[10];
- int BunionD[15];
- int AintersectB[10];
- int AintersectC[10];
- int * lastElement;
- ostream_iterator<int> screen(cout," ");
- cout << "setA:" << endl;
- copy(setA,setA+5,screen);
- cout << endl;
- cout << "setB:" << endl;
- copy(setB,setB+7,screen);
- cout << endl;
- cout << "setC:" << endl;
- copy(setC,setC+5,screen);
- cout << endl;
- cout << "setD:" << endl;
- copy(setD,setD+6,screen);
- cout << endl;
- // set_union
- lastElement = set_union(setA,setA+5,setB,setB+7,AunionB);
- cout << "AunionB:" << endl;
- copy(AunionB,lastElement,screen);
- cout << endl;
- lastElement = set_union(setA,setA+5,setC,setC+5,AunionC);
- cout << "AunionC:" << endl;
- copy(AunionC,lastElement,screen);
- cout << endl;
- lastElement = set_union(setB,setB+7,setD,setD+6,BunionD);
- cout << "BunionD:" << endl;
- copy(BunionD,lastElement,screen);
- cout << endl;
- // set_intersection
- lastElement = set_intersection(setA, setA+5,setB,setB+7,AintersectB);
- cout << "AintersectB:" << endl;
- copy(AintersectB,lastElement,screen);
- cout << endl;
- lastElement = set_intersection(setA, setA+5,setC,setC+5,AintersectC);
- cout << "AintersectC:" << endl;
- copy(AintersectC,lastElement,screen);
- cout << endl;
- return 0;
- }
运行结果:
charList:
a b c d e
charList:
A B C D E
list
2 8 5 1 7 11 3
4 16 10 2 14 22 6
list
4 16 10 2 14 22 6
3 set_difference,set_symmetric_difference
- #include <algorithm>
- template <class inputItr1, class inputItr2,class outputItr>
- outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
- template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
- outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
- template <class inputItr1, class inputItr2,class outputItr>
- outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
- template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
- outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main() {
- int setA[5] = {2,4,5,7,8};
- int setB[7] = {3,4,5,6,7,8,10};
- int setC[5] = {1,5,6,8,15};
- int AdifferenceC[5];
- int BsymDiffC[10];
- int * lastElement;
- ostream_iterator<int> screen(cout," ");
- cout << "setA:" << endl;
- copy(setA,setA+5,screen);
- cout << endl;
- cout << "setB:" << endl;
- copy(setB,setB+7,screen);
- cout << endl;
- cout << "setC:" << endl;
- copy(setC,setC+5,screen);
- cout << endl;
- // set_difference
- lastElement = set_difference(setA,setA+5,setC,setC+5,AdifferenceC);
- cout << "AdifferenceC:" << endl;
- copy(AdifferenceC,lastElement,screen);
- cout << endl;
- // set_symmetric_differenc 对称差
- lastElement = set_symmetric_difference(setB,setB+7,setC,setC+5,BsymDiffC);
- cout << "BsymDiffC:" << endl;
- copy(BsymDiffC,lastElement,screen);
- cout << endl;
- return 0;
- }
2 4 5 7 8
setB:
3 4 5 6 7 8 10
setC:
1 5 6 8 15
AdifferenceC:
2 4 7
BsymDiffC:
1 3 4 7 10 15
STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference相关推荐
- STL源码剖析 Set相关算法 并集 set_union|交集 set_intersection|差集 set_difference |对称差集 set_symmetric_difference
注意事项 四种相关算法:并集.交集.差集.对称差集 本章的四个算法要求元素不可以重复并且经过了排序 底层接受STL的set/multiset容器作为输入空间 不接受底层为hash_set和hash_m ...
- STL之set_union、set_intersection、set_difference、set_symmetric_difference
需要的头文件: algorithm 源码: //源码比较复杂,不予列出 //有兴趣的可以参考sgi stl的源码 作用: 计算两个集合的并集.交集.差集.对称差集. 通过二元仿函数我们可以取代 ope ...
- 第九层(16):STL终章——常用集合算法
文章目录 前情回顾 常用集合算法 set_intersection set_union set_difference 最后一座石碑倒下,爬塔结束 一点废话
- Effective STL 50条有效使用STL的经验笔记
Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...
- C++学习笔记-STL
C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 ...
- 《Effective STL》条款解读
条款01:慎重选择容器类型 vector.list和deque有着不同的复杂度,vector是默认使用的序列类型.当需要频繁在序列中间做插入和删除操作时,应使用list.当大多数插入和删除操作发生在序 ...
- Effective STL 读书笔记
Effective STL 读书笔记 标签(空格分隔): 未分类 慎重选择容器类型 标准STL序列容器: vector.string.deque和list(双向列表). 标准STL管理容器: set. ...
- 《Effective STL》中文版 读书笔记
50条有效使用STL的经验 第一条 慎重选择容器类型(20190713) 第二条 不要试图编写独立于容器类型的代码(20190713) 第三条 确保容器中的对象副本正确而高效(20190713) 第四 ...
- 【绝版C++书籍】《Effective STL》读书笔记
<Effective STL>读书笔记 写在前面 0<Effective STL>中可能过时的内容 1 容器 第1条:慎重选择容器类型. 第2条:不要试图编写独立于容器类型的代 ...
最新文章
- 有源汇上下界最小费用可行流 ---- P4553 80人环游世界(拆点 + 有源汇上下界最小费用可行流)
- cgo引用定义不一致_应急照明、消防应急照明和疏散指示系统的定义与分类
- Activity动画效果笔记
- delphi初级教程之delphi断点调试一
- 为什么需要MapReduce?
- Python面试题总结(6)--数据类型(综合)
- linux安装selenium+chrome+phantomjs
- 【word】为什么word分两栏的最后一页左边一栏没写完跑到右边去了
- 黑盒测试与白盒测试的区别与方法
- 国内主要Android应用市场包名大全
- 在Windows 7中使用AppLocker限制对程序的访问
- composer install 中出现用户名密码错误问题的解决方法
- 【karle 笔记】无法通过Windows功能控制面板自动安装或卸载Windows Server 角色和功能。
- prometheus告警功能
- 什么是mysql 中级工程师的理解
- Matlab 基础04 - 冒号Colon operator “:”的使用和复杂应用详析
- 互联网电视变身哄娃神器:YOYO搜片真方便
- 带一张阿拉旅游卡,随时出发
- 编写一个程序,实现一个证券交易软件的登录界面。
- FL Studio21中文完整版升级下载