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

声明:

  1. #include <algorithm>
  2. template <class inputItr1, class inputItr2>
  3. bool includes(inputItr1 first1, inputItr1 last1, inputItr2 first2, inputItr2 last2);
  4. template <class inputItr1, class inputItr2, class binaryPredicate>
  5. bool includes(inputItr1 first1, inputItr1 last1, inputItr2,binaryPredicate op);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. int main() {
  11. char setA[5] = {'A','B','C','D','E'};
  12. char setB[10] = {'A','B','C','D','E','F','I','J','K','L'};
  13. char setC[5] = {'A','E','I','O','U'};
  14. ostream_iterator<char> screen(cout, " ");
  15. cout << "setA:" << endl;
  16. copy(setA,setA+5,screen);
  17. cout << endl;
  18. cout << "setB:" << endl;
  19. copy(setB,setB+10,screen);
  20. cout << endl;
  21. cout << "setC:" << endl;
  22. copy(setC,setC+5,screen);
  23. cout << endl;
  24. if (includes(setB,setB+10,setA,setA+5))
  25. {
  26. cout << "setA is a subset of setB" << endl;
  27. } else {
  28. cout << "setA is not a subset of setB" << endl;
  29. }
  30. if (includes(setB,setB+10,setC,setC+5))
  31. {
  32. cout << "setC is a subset of setB" << endl;
  33. } else {
  34. cout << "setC is not a subset of setB" << endl;
  35. }
  36. return 0;
  37. }

运行结果:

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

声明:

  1. #include <algorithm>
  2. template <class inputItr1, class inputItr2,class outputItr>
  3. outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
  4. template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
  5. outputItr set_intersection(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
  6. template <class inputItr1, class inputItr2,class outputItr>
  7. outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
  8. template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
  9. outputItr set_union(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. int main() {
  11. int setA[5] = {2,4,5,7,8};
  12. int setB[7] = {1,2,3,4,5,6,7};
  13. int setC[5] = {2,5,8,8,15};
  14. int setD[6] = {1,4,4,6,7,12};
  15. int AunionB[10];
  16. int AunionC[10];
  17. int BunionD[15];
  18. int AintersectB[10];
  19. int AintersectC[10];
  20. int * lastElement;
  21. ostream_iterator<int> screen(cout," ");
  22. cout << "setA:" << endl;
  23. copy(setA,setA+5,screen);
  24. cout << endl;
  25. cout << "setB:" << endl;
  26. copy(setB,setB+7,screen);
  27. cout << endl;
  28. cout << "setC:" << endl;
  29. copy(setC,setC+5,screen);
  30. cout << endl;
  31. cout << "setD:" << endl;
  32. copy(setD,setD+6,screen);
  33. cout << endl;
  34. // set_union
  35. lastElement = set_union(setA,setA+5,setB,setB+7,AunionB);
  36. cout << "AunionB:" << endl;
  37. copy(AunionB,lastElement,screen);
  38. cout << endl;
  39. lastElement = set_union(setA,setA+5,setC,setC+5,AunionC);
  40. cout << "AunionC:" << endl;
  41. copy(AunionC,lastElement,screen);
  42. cout << endl;
  43. lastElement = set_union(setB,setB+7,setD,setD+6,BunionD);
  44. cout << "BunionD:" << endl;
  45. copy(BunionD,lastElement,screen);
  46. cout << endl;
  47. // set_intersection
  48. lastElement = set_intersection(setA, setA+5,setB,setB+7,AintersectB);
  49. cout << "AintersectB:" << endl;
  50. copy(AintersectB,lastElement,screen);
  51. cout << endl;
  52. lastElement = set_intersection(setA, setA+5,setC,setC+5,AintersectC);
  53. cout << "AintersectC:" << endl;
  54. copy(AintersectC,lastElement,screen);
  55. cout << endl;
  56. return 0;
  57. }

运行结果:

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

声明:
  1. #include <algorithm>
  2. template <class inputItr1, class inputItr2,class outputItr>
  3. outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
  4. template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
  5. outputItr set_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);
  6. template <class inputItr1, class inputItr2,class outputItr>
  7. outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst);
  8. template <class inputItr1, class inputItr2,class outputItr,class binaryPredicate>
  9. outputItr set_symmetric_difference(inputItr1 first1, inputItr1 last1, inputItr2 first2,inputItr2 last2,outputItr destFirst,binaryPredicate op);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. int main() {
  11. int setA[5] = {2,4,5,7,8};
  12. int setB[7] = {3,4,5,6,7,8,10};
  13. int setC[5] = {1,5,6,8,15};
  14. int AdifferenceC[5];
  15. int BsymDiffC[10];
  16. int * lastElement;
  17. ostream_iterator<int> screen(cout," ");
  18. cout << "setA:" << endl;
  19. copy(setA,setA+5,screen);
  20. cout << endl;
  21. cout << "setB:" << endl;
  22. copy(setB,setB+7,screen);
  23. cout << endl;
  24. cout << "setC:" << endl;
  25. copy(setC,setC+5,screen);
  26. cout << endl;
  27. // set_difference
  28. lastElement = set_difference(setA,setA+5,setC,setC+5,AdifferenceC);
  29. cout << "AdifferenceC:" << endl;
  30. copy(AdifferenceC,lastElement,screen);
  31. cout << endl;
  32. // set_symmetric_differenc 对称差
  33. lastElement = set_symmetric_difference(setB,setB+7,setC,setC+5,BsymDiffC);
  34. cout << "BsymDiffC:" << endl;
  35. copy(BsymDiffC,lastElement,screen);
  36. cout << endl;
  37. return 0;
  38. }
运行结果:
setA:
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相关推荐

  1. STL源码剖析 Set相关算法 并集 set_union|交集 set_intersection|差集 set_difference |对称差集 set_symmetric_difference

    注意事项 四种相关算法:并集.交集.差集.对称差集 本章的四个算法要求元素不可以重复并且经过了排序 底层接受STL的set/multiset容器作为输入空间 不接受底层为hash_set和hash_m ...

  2. STL之set_union、set_intersection、set_difference、set_symmetric_difference

    需要的头文件: algorithm 源码: //源码比较复杂,不予列出 //有兴趣的可以参考sgi stl的源码 作用: 计算两个集合的并集.交集.差集.对称差集. 通过二元仿函数我们可以取代 ope ...

  3. 第九层(16):STL终章——常用集合算法

    文章目录 前情回顾 常用集合算法 set_intersection set_union set_difference 最后一座石碑倒下,爬塔结束 一点废话

  4. Effective STL 50条有效使用STL的经验笔记

    Scott Meyers大师Effective三部曲:Effective C++.More Effective C++.Effective STL,这三本书出版已很多年,后来又出版了Effective ...

  5. C++学习笔记-STL

    C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), lists(链表), 和 ...

  6. 《Effective STL》条款解读

    条款01:慎重选择容器类型 vector.list和deque有着不同的复杂度,vector是默认使用的序列类型.当需要频繁在序列中间做插入和删除操作时,应使用list.当大多数插入和删除操作发生在序 ...

  7. Effective STL 读书笔记

    Effective STL 读书笔记 标签(空格分隔): 未分类 慎重选择容器类型 标准STL序列容器: vector.string.deque和list(双向列表). 标准STL管理容器: set. ...

  8. 《Effective STL》中文版 读书笔记

    50条有效使用STL的经验 第一条 慎重选择容器类型(20190713) 第二条 不要试图编写独立于容器类型的代码(20190713) 第三条 确保容器中的对象副本正确而高效(20190713) 第四 ...

  9. 【绝版C++书籍】《Effective STL》读书笔记

    <Effective STL>读书笔记 写在前面 0<Effective STL>中可能过时的内容 1 容器 第1条:慎重选择容器类型. 第2条:不要试图编写独立于容器类型的代 ...

最新文章

  1. 有源汇上下界最小费用可行流 ---- P4553 80人环游世界(拆点 + 有源汇上下界最小费用可行流)
  2. cgo引用定义不一致_应急照明、消防应急照明和疏散指示系统的定义与分类
  3. Activity动画效果笔记
  4. delphi初级教程之delphi断点调试一
  5. 为什么需要MapReduce?
  6. Python面试题总结(6)--数据类型(综合)
  7. linux安装selenium+chrome+phantomjs
  8. 【word】为什么word分两栏的最后一页左边一栏没写完跑到右边去了
  9. 黑盒测试与白盒测试的区别与方法
  10. 国内主要Android应用市场包名大全
  11. 在Windows 7中使用AppLocker限制对程序的访问
  12. composer install 中出现用户名密码错误问题的解决方法
  13. 【karle 笔记】无法通过Windows功能控制面板自动安装或卸载Windows Server 角色和功能。
  14. prometheus告警功能
  15. 什么是mysql 中级工程师的理解
  16. Matlab 基础04 - 冒号Colon operator “:”的使用和复杂应用详析
  17. 互联网电视变身哄娃神器:YOYO搜片真方便
  18. 带一张阿拉旅游卡,随时出发
  19. 编写一个程序,实现一个证券交易软件的登录界面。
  20. FL Studio21中文完整版升级下载

热门文章

  1. RocketMQ的历史发展
  2. 数据库-数据类型介绍
  3. 数据库-表中导入数据-insert
  4. 物理设计-数据类型的选择
  5. 使用Github(基本概念实战操作)
  6. 200827C阶段一_C++基础
  7. 200725学习日报循环语句和数组
  8. 记不住ASP.NET页面生命周期的苦恼
  9. POJ 3259 Wormholes【最短路/SPFA判断负环模板】
  10. 在Visual Studio的Server Explorer中怎样修改表名