STL源码剖析学习十四:算法之set相关算法
STL中定义的set要求元素不得重复且已经排序
set算法要求的都是有序区间,但元素可以重复出现
另外提供的hash版本的set因为其元素无序,因此不能作为set函数的参数
set算法前4个参数分别表示两个区间,第五个参数表示存放结果的区间的起始位置。
还允许用户指定a<b的意义,判断两个元素是否相等全靠小于运算
先给个例子
#include<set> #include<iostream> #include<algorithm> #include<iterator> using namespace std;template <class T> struct display {void operator()(const T& x){cout<<x<<" ";} };int main() {int ia1[6] = {1,3,5,7,9,11};int ia2[7] = {1,2,3,5,8,13,1};multiset<int> s1(ia1, ia1+6);multiset<int> s2(ia2, ia2+7);for_each(s1.begin(), s1.end(), display<int>());cout<<endl;for_each(s2.begin(), s2.end(), display<int>());cout<<endl;cout<<"union"<<endl;set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));cout<<endl;cout<<"intersection"<<endl;set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));cout<<endl;cout<<"difference"<<endl;set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));cout<<endl;cout<<"difference"<<endl;set_difference(s2.begin(), s2.end(), s1.begin(), s1.end(), ostream_iterator<int>(cout, " "));cout<<endl;cout<<"symmetric difference"<<endl;set_symmetric_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));cout<<endl;system("pause"); }
求两个集合的并集,是一种稳定操作,输入区间内的每个元素相对顺序都不会改变
set_union(first1, last1, first2, last2, result) {while(first1!=last1 && first2!=last2){//两个容器内的元素相比较,把较小者放入结果容器中,并向前移动迭代器//如果两个元素相等则写入结果中,同时移动两个迭代器if(*first1<*first2){*result = *first;++first1;}else if(*first2<*first1){*result = *first;++first2;}else{*result = *first1;++first1;++first2;}++result;}//把剩余的元素都拷贝进结果中//copy返回迭代器指向目标容器的插入元素的最后一个元素的下一个元素return copy(first2, last2, copy(first1, last1, result)); }
求交集,若某个值在s1中出现n次,在s2中出现m次,在输出中会出现min(m,n)次
set_intersection(first1, last1, first2, last2, result) {while(first1!=last1 && first2!=last2){if(*first1<*first2){++first1;}else if(*first2<*first1){++first2;}else{*result = *first1;++first1;++first2;++result;}}return result; }
求差集,若某个值在s1中出现n次,在s2中出现m次,在输出中会出现max(n-m,0)次
set_difference(first1, last1, first2, last2, result) {while(first1!=last1 && first2!=last2){if(*first1<*first2){*result = *first1;++first;++result;}else if(*first2<*first1){++*first2;}else{++first1;++first2;}}return copy(first1, last1, result); }
求对称差集 (s1-s2)∪(s2-s1)若某个值在s1中出现n次,在s2中出现m次,在输出中会出现abs(m-n)次
set_symmetric_difference(first1, last1, first2, last2, result) {while(first1!=last1 && first2!=last2){if(*first1<*first2){*result = *first1;++first1;++result;}if(*first2<*first1){*result = *first2;++first2;++result;}else{++first1;++first2;}}return copy(first1, last1, copy(first2, last2, result)); }
转载于:https://www.cnblogs.com/w0w0/archive/2012/04/27/2472999.html
STL源码剖析学习十四:算法之set相关算法相关推荐
- STL源码剖析学习二:空间配置器(allocator)
STL源码剖析学习二:空间配置器(allocator) 标准接口: vlaue_type pointer const_pointer reference const_reference size_ty ...
- STL源码剖析学习七:stack和queue
STL源码剖析学习七:stack和queue stack是一种先进后出的数据结构,只有一个出口. 允许新增.删除.获取最顶端的元素,没有任何办法可以存取其他元素,不允许有遍历行为. 缺省情况下用deq ...
- STL源码剖析学习之increment、decrement、dereference实现源码
//STL之increment.decrement.dereference实现源码 //学习目的:STL实现原理.操作符(++i,i++,*等操作符的重载) //increment/dereferen ...
- STL源码剖析—学习记录
提示:建议看完侯捷老师的STL标准库视频后,再看这本书,会看的下去一些!本人对其中重点内容和概念进行了提取,希望对一起前行的人有些许帮助,码字不易,欢迎点个赞呦! 文章目录 一.可能令你困惑的C++语 ...
- redis源码剖析(十四)—— dump.rdb文件分析工具
分析rdb文件的工具 安装 git clone https://github.com/sripathikrishnan/redis-rdb-tools.git sudo pip install --u ...
- STL源码剖析 数值算法 copy 算法
copy复制操作,其操作通过使用assignment operator .针对使用trivial assignment operator的元素型别可以直接使用内存直接复制行为(使用C函数 memove ...
- STL源码剖析 slist单向链表概述
概述 SGI STL的list是一个双向链表,单向链表是slist,其不在标准规格之内 单向和双向链表的区别在于,单向链表的迭代器是单向的 Forward Iterator,双向链表的迭代器属于双向的 ...
- 《STL源码剖析》学习--6章--_rotate算法分析
最近在看侯捷的<STL源码剖析>,其中有许多不太明白之处,后经分析或查找资料有了些理解,现记录一下. <STL源码剖析>学习--6章--random access ite ...
- 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】
今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现! 话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://b ...
最新文章
- 27场机器学习面试后,来划个概念重点吧
- slide简介(大数据技术)
- fetch移动端浏览器兼容问题
- STM32工作笔记0066---待机唤醒实验-设备低功耗-M3H
- RxJava操作符lift笔记25
- mysql时间总结_MYSQL日期时间总结
- 丁小平:人类究竟需要什么样的微积分原理
- Python源码保护
- 【Python】UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe
- 最小二乘法求直线的理解
- 计算机色温调整,电脑色温如何调节
- 重磅干货:30张图读懂当前中国金融体系!
- linux 进程 网速监控
- PreScan快速入门到精通第三十二讲基于PreScan进行毫米波雷达传感器仿真
- 【Java岗】9月华为校招+阿里巴巴社招完整面经
- 归因分析:淘宝直播数据助理及其价值研究
- 深度学习各类性能指标含义解释
- CSS实现“大于号”或“小于号”箭头
- linux 测试cpu工具,给centos安装cpu监测工具
- 武汉经开区2021年知识产权资助(奖励)资金开始申报
热门文章
- mysql 命令行执行存储过程_mysql 命令行执行存储过程
- src refspec xxx does not match any
- Windows下后台运行cmd启动的程序
- Bootstrap tab页签刷新加载不显示,只有点击其他标签后第一个才显示
- 12v电源正负极区分_弱电工程UPS电源如何安装?如何配置计算?故障如何处理?...
- 【CSP201312-1 】出现次数最多的数,排序后扫描并记录
- 【codevs1380】没有上司的舞会
- C++奥赛一本通排序题解
- NYOJ241 - 字母统计
- SQL Server 函数的使用(转换函数)