注意事项

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

并集 set_union

  • s1 U s2
  • 考虑到s1 和 s2中每个元素都不唯一,如果单个元素在S1中出现了m次,在s2中出现了n次,那么该数值在并集区间内出现的次数是 max(n,m),假设m小于n。那么m个数来自s1,m-n个数来自s2
  • 稳定算法 相对次序不会改变
  • 版本一使用 operator <
  • 版本二使用 comp 进行比较
  • 注意:set已经排好顺序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_union(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){//如果均没有到达末尾//在两个区间内分别移动迭代器,首先将元素较小的数值(假设为A区)记录于目标区,移动A区迭代器使其前进,同一时间B区迭代器保持不变//如果两个迭代器指向的元素的大小是一致的,均移动两个迭代器while(true){//只要两个区间中有一个到达末尾就需要结束循环//将尚未到达尾端的区间的所有剩余元素拷贝到目的端//此刻的[first1,last1)和[first2,last2)之中有一个是空白的区间if (first1==last1)return std::copy(first2,last2,result);if (first2==last2)return std::copy(first1,last1,result);if (*first1 < *first2){*result = *first1;++first1;} else if(*first1 > *first2){*result = *first2;++first2;} else{ //*first1 == *first2*result = *first1;++first1;++first2;}++result;}}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_union(first,first+5,second,second+5,v.begin()); // 5 10 15 20 25 30 40 50  0  0v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50std::cout << "The union has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 8 elements:
//5 10 15 20 25 30 40 50
//
//Process finished with exit code 0

交集 set_intersection

  • 构造元素的交集,即同时出现在两个集合中的元素
  • 返回迭代器指向输出区间的尾端
  • 考虑到s1 和 s2中每个元素都不唯一,如果单个元素在S1中出现了m次,在s2中出现了n次,那么该数值在交集区间内出现的次数是 min(n,m)
  • 稳定算法 相对次序不会改变
  • 版本一使用 operator <
  • 版本二使用 comp 进行比较
  • 注意:set已经排好顺序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_intersection(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while(first1!=last1 && first2!=last2){if (*first1 < *first2)++first1;else if (*first1 > *first2){++first2;} else{*result = *first1;++first1;++first2;++result;}}return result;
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_intersection(first,first+5,second,second+5,v.begin()); // 10 20 0  0  0  0  0  0  0  0v.resize(it-v.begin()); // 10 20std::cout << "The intersection has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//10 20
//
//Process finished with exit code 0

差集 set_difference

  • 构造集合S1 - S2。此集合包含出现于S1但是不出现于S2的每一个元素
  • 返回迭代器指向输出区间的尾端
  • 考虑到s1 和 s2中每个元素都不唯一,如果单个元素在S1中出现了m次,在s2中出现了n次,那么该数值在交集区间内出现的次数是 max(m-n,0)
  • 稳定算法 相对次序不会改变
  • 版本一使用 operator <
  • 版本二使用 comp 进行比较
  • 注意:set已经排好顺序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while(first1!=last1 && first2!=last2){if (*first1 < *first2){*result = *first1;++result;++first1;}else if (*first1 > *first2){++first2;} else{++first1;++first2;}}return std::copy(first1,last1,result);
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_difference(first,first+5,second,second+5,v.begin()); // 5 15 25  0  0  0  0  0  0  0v.resize(it-v.begin()); // 5 15 25std::cout << "The difference has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//5 15 25
//
//Process finished with exit code 0

对称差集 set_symmetric_difference

  • 构造出 (s1 - s2) U (s2 - s1)
  • 打印出现于s1但是不出现于s2 以及出现于s2但是不出现于s1的每一个元素
  • 考虑到s1 和 s2中每个元素都不唯一,如果单个元素在S1中出现了m次,在s2中出现了n次,那么该数值在交集区间内出现的次数是 | n - m|
  • 稳定算法 相对次序不会改变
  • 版本一使用 operator <
  • 版本二使用 comp 进行比较
  • 注意:set已经排好顺序
template <class InputIterator1,class InputIterator2,class OutputIterator>
OutputIterator set_symmetric_difference(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result){while (true){//以下两个条件不会同时成立 即if (first1 == last1){return std::copy(first2,last2,result);}if (first2 == last2){return std::copy(first1,last1,result);}if (*first1 < *first2){*result = *first1;++result;++first1;}else if (*first1 > *first2){*result = *first2;++result;++first2;} else{++first1;++first2;}}
}
int main(){int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int>v(10);std::vector<int>::iterator it;std::sort(first,first+5);      //5,10,15,20,25std::sort(second,second+5);  //10,20,30,40,50it = std::set_symmetric_difference(first,first+5,second,second+5,v.begin()); //5 15 25 30 40 50  0  0  0  0v.resize(it-v.begin()); // 5 15 25 30 40 50std::cout << "The set_symmetric_difference has " << (v.size()) << " elements:\n";for (auto tmp : v) {std::cout << tmp << " ";}std::cout << std::endl;
}///Users/chy/Desktop/exceptional/cmake-build-debug/exceptional
//The union has 2 elements:
//5 15 25 30 40 50
//
//Process finished with exit code 0

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

  1. STL源码剖析 set相关算法

    STL 一共提供了四种与set (集合)相关的算法,分别是并集(union).交集(intersection) > 差集 (difference).对称差集 (symmetricdifferen ...

  2. 《STL源码剖析》相关面试题总结

    一.STL简介 STL提供六大组件,彼此可以组合套用: 容器 容器就是各种数据结构,我就不多说,看看下面这张图回忆一下就好了,从实现角度看,STL容器是一种class template. 算法 各种常 ...

  3. STL源码剖析 数值算法 copy 算法

    copy复制操作,其操作通过使用assignment operator .针对使用trivial assignment operator的元素型别可以直接使用内存直接复制行为(使用C函数 memove ...

  4. STL源码学习----集合相关算法

    STL一共提供了四种与集合相关的算法,分别是并集(union), 交集(intersection),差集(difference),对称差集(symmetric difference). 这四种集合算法 ...

  5. STL源码剖析 数值算法 heap算法

    算法 adjacent_find count count_if find find_if find_end for_each generate generate_n includes max_elem ...

  6. STL源码剖析 数值算法 copy_backward 算法

    copy_backward 时间技巧和copy类似 主要是将[first,last)区间范围内的元素按照逆行方向复制到以result-1为起点,方向同样是逆行的区间上 返回的迭代器的类型是result ...

  7. STL源码剖析面试问题

    当vector的内存用完了,它是如何动态扩展内存的?它是怎么释放内存的?用clear可以释放掉内存吗?是不是线程安全的? vector内存用完了,会以当前size大小重新申请2* size的内存,然后 ...

  8. STL源码剖析 算法开篇

    STL源码剖析 算法章节 算法总览_CHYabc123456hh的博客-CSDN博客 质变算法 质变算法 - 会改变操作对象的数值,比如互换.替换.填写.删除.排列组合.分隔.随机重排.排序等 #in ...

  9. 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】

    今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现! 话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://b ...

最新文章

  1. Linux怎么互相ping通,主机+虚拟机Ubuntu+开发板互相ping通
  2. 8个试剂,其中一个有毒,最少多少只小白鼠能检测出有毒试剂——分而治之思想...
  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)
  4. Taro+react开发(17)--赋值错误回显异常
  5. pg函数同步数据到mysql_将数据从PostgreSQL复制到MySQL
  6. 测试 极客时间_针对数据极客和记者测试DocHive
  7. Leetcode每日一题:107.binary-tree-level-order-traversal-ii(二叉树的层次遍历)
  8. django模板层 (标签,过滤器,自定义inclusion_tag,模板的继承与导入)
  9. matlab2c使用c++实现matlab函数系列教程-deconv函数
  10. [转] - 浅谈数据分析和数据建模
  11. Java中的命名规范总结
  12. HTML导入字体并使用
  13. python图片裁剪
  14. C语言笔记:进制转换与32位二进制IP地址转换十进制问题
  15. 一个工科土博士生---最近的状态和想法
  16. 2020 软件测试行业问卷调查结果分析
  17. 开篮球馆需要什么_建立一个篮球馆需要什么!
  18. 【试算法题梳理】——二叉树
  19. windows - DirectX渲染的学习总结
  20. html5旋转的向日葵动画js特效

热门文章

  1. python遍历循环中的遍历结构可以是什么_(一)Python入门-4控制语句:06for循环结构-遍历各种可迭代对象-range对象...
  2. php中拼接html代码,如何利用ajax给html动态拼接代码
  3. 2.2基本算法之递归和自调用函数_数据结构与算法之5——队列和栈
  4. 前端和后端的英文_计算机专业的本科生在前端、后端、测试和运维之间该如何选择...
  5. LSGO软件技术团队对外技术交流
  6. 【转】[完全免费] 在线UML Sequence Diagram 时序图工具 - 教程第3部分
  7. a.pop啥意思python_python中pop什么意思
  8. python输入直角三角形a、b、输出斜边c_编写一个程序,输入直角三角形两条直角边a和b的长度,利用勾股定理计算斜边c的长度。要求结果保留2位...
  9. 设计模式(六)J2EE 模式
  10. CCIE理论-第六篇-SD-WAN网络(一)