STL学习笔记----15.STL算法之 (已序区间算法)
来源:互联网 发布:内网映射软件 编辑:程序博客网 时间:2024/06/05 03:18
一. 概述
都是针对已序区间执行的算法。
判断某区间内是否包含某个元素includes()判断某区间内的每一个元素是否都涵盖于另一区间中lower_bound()搜索第一个"大于等于给定值"的元素upper _bound()搜索第一个"大于给定值"的元素equal_range()返回"等于给定值"的所有元素构成的区间merge()将两个区间合并set_union()求两个区间的并集set_intersection()求两个区间的交集set_difference()求位于第一区间但不位于第二区间的所有元素,形成一个已序区间set_symmetric_difference()找出只出现于两区间之一的所有元素,形成一个已序区间inplace_merge()将两个连续的已序区间合并二. 搜索元素
1. 检查某个元素是否存在
- //判断已序区间[beg, end)是否包含和value等值的元素
- bool
- binary_search (ForwardIterator beg, ForwardIterator end,
- const T& value)
- bool
- binary_search (ForwardIterator beg, ForwardIterator end,
- const T& value,
- BinaryPredicate op)
//判断已序区间[beg, end)是否包含和value等值的元素boolbinary_search (ForwardIterator beg, ForwardIterator end, const T& value)boolbinary_search (ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)2. 检查若干个值是否存在
- //判断已序区间[beg, end)是否包含另一个已序区间[searchBeg, searchEnd)
- bool
- includes (InputIterator1 beg, InputIterator1 end,
- InputIterator2 searchBeg, InputIterator2 searchEnd)
- bool
- includes (InputIterator1 beg, InputIterator1 end,
- InputIterator2 searchBeg, InputIterator2 searchEnd,
- BinaryPredicate op)
//判断已序区间[beg, end)是否包含另一个已序区间[searchBeg, searchEnd)boolincludes (InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd)boolincludes (InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd, BinaryPredicate op)3. 搜索第一个或最后一个可能位置
- //返回第一个"大于等于value"的元素位置,如果不存在返回end
- ForwardIterator
- lower_bound (ForwardIterator beg, ForwardIterator end,
- const T& value)
- ForwardIterator
- lower_bound (ForwardIterator beg, ForwardIterator end,
- const T& value,
- BinaryPredicate op)
- //返回第一个"大于value"的元素位置
- ForwardIterator
- upper_bound (ForwardIterator beg, ForwardIterator end,
- const T& value)
- ForwardIterator
- upper_bound (ForwardIterator beg, ForwardIterator end,
- const T& value,
- BinaryPredicate op)
//返回第一个"大于等于value"的元素位置,如果不存在返回endForwardIteratorlower_bound (ForwardIterator beg, ForwardIterator end, const T& value)ForwardIteratorlower_bound (ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)//返回第一个"大于value"的元素位置ForwardIteratorupper_bound (ForwardIterator beg, ForwardIterator end, const T& value)ForwardIteratorupper_bound (ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)4. 搜索第一个和最后一个可能位置
- //返回"与value相等"的元素所形成的区间
- pair<ForwardIterator,ForwardIterator>
- equal_range (ForwardIterator beg, ForwardIterator end,
- const T& value)
- pair<ForwardIterator,ForwardIterator>
- equal_range (ForwardIterator beg, ForwardIterator end,
- const T& value,
- BinaryPredicate op)
//返回"与value相等"的元素所形成的区间pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator beg, ForwardIterator end, const T& value)pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)三. 合并元素
1. 两个已序集合的总合(Sum)
- //source1: 1 2 2 4 6 7 7 9
- //source2: 2 2 2 3 6 6 8 9
- //Sum: 1 2 2 2 2 2 3 4 6 6 6 7 7 8 9 9
- //将源区间[source1Beg, source1End)和[source2Beg, source2End)内的元素合并
- //以destBeg为起点输出
- OutputIterator
- merge (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- Output Iterator destBeg)
- OutputIterator
- merge (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg,
- BinaryPredicate op)
//source1: 1 2 2 4 6 7 7 9//source2: 2 2 2 3 6 6 8 9//Sum: 1 2 2 2 2 2 3 4 6 6 6 7 7 8 9 9//将源区间[source1Beg, source1End)和[source2Beg, source2End)内的元素合并//以destBeg为起点输出OutputIteratormerge (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, Output Iterator destBeg)OutputIteratormerge (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
2.. 两个已序集合的并集(Union)
- //source1: 1 2 2 4 6 7 7 9
- //source2: 2 2 2 3 6 6 8 9
- //Union: 1 2 2 2 3 4 6 6 7 7 8 9
- //这个区间内含的元素要不来自第一源区间,要不来自第二源区间
- OutputIterator
- set_union (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg)
- OutputIterator
- set_union (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg,
- BinaryPredicate op)
//source1: 1 2 2 4 6 7 7 9//source2: 2 2 2 3 6 6 8 9//Union: 1 2 2 2 3 4 6 6 7 7 8 9//这个区间内含的元素要不来自第一源区间,要不来自第二源区间OutputIteratorset_union (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg)OutputIteratorset_union (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)3. 两个已序集合的交集(Intersection)
- //source1: 1 2 2 4 6 7 7 9
- //source2: 2 2 2 3 6 6 8 9
- //Inter: 2 2 6 9
- //元素必须同时在两个区间
- OutputIterator
- set_intersection (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg)
- OutputIterator
- set_intersection (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator sotirce2End,
- OutputIterator destBeg,
- BinaryPredicate op)
//source1: 1 2 2 4 6 7 7 9//source2: 2 2 2 3 6 6 8 9//Inter: 2 2 6 9//元素必须同时在两个区间OutputIteratorset_intersection (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg)OutputIteratorset_intersection (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator sotirce2End, OutputIterator destBeg, BinaryPredicate op)4. 两个已序集合的差集(Difference)
- //source1: 1 2 2 4 6 7 7 9
- //source2: 2 2 2 3 6 6 8 9
- //differ: 1 4 7 7
- //只在第一个区间,不在第二个区间
- OutputIterator
- set_difference (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg)
- OutputIterator
- set_difference (InputIterator source1Beg, InputIterator source1End,
- InputIterator source2Beg, InputIterator source2End,
- OutputIterator destBeg,
- BinaryPredicate op)
//source1: 1 2 2 4 6 7 7 9//source2: 2 2 2 3 6 6 8 9//differ: 1 4 7 7//只在第一个区间,不在第二个区间OutputIteratorset_difference (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg)OutputIteratorset_difference (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)5. 将连贯的已序区间合并
- //将已序[beg1, end1beg2)和[end1beg2, end2)的元素合并,
- //使区间[beg1, end2)成为两者之和
- void
- inplace_merge (BidirectionalIterator beg1,
- BidirectionalIterator end1beg2,
- BidirectionalIterator end2)
- void
- inplace_merge (BidirectionalIterator beg1,
- BidirectionalIterator end1beg2,
- BidirectionalIterator end2,
- BinaryPredicate op)
//将已序[beg1, end1beg2)和[end1beg2, end2)的元素合并,//使区间[beg1, end2)成为两者之和voidinplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2, BidirectionalIterator end2)voidinplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2, BidirectionalIterator end2, BinaryPredicate op)
- STL学习笔记----15.STL算法之 (已序区间算法)
- STL学习笔记----15.STL算法之 (已序区间算法)
- STL算法之heap算法,已排序区间算法
- STL学习笔记----11.STL算法之 (变动性算法)
- STL学习笔记----13.STL算法之 (变序性算法)
- STL学习笔记----14.STL算法之 (排序算法)
- STL学习笔记----16.STL算法之 (数值算法)
- STL学习笔记----16.STL算法之 (数值算法)
- STL学习笔记----14.STL算法之 (排序算法)
- STL学习笔记----13.STL算法之 (变序性算法)
- STL学习笔记----11.STL算法之 (变动性算法)
- STL学习笔记----9.STL算法之 for_each()
- STL学习笔记----9.STL算法之 for_each()
- SGI-STL学习笔记之heap算法
- STL学习笔记之sort算法
- STL学习笔记之算法--algorithmn
- STL学习笔记--排序算法
- STL学习笔记----10.STL算法之 (非变动性算法)
- Could not initialize class sun.awt.X11GraphicsEnvironment
- moonseo-占座:网站关键词多怎么做优化?
- tomcat虚拟目录配置
- 网警找人流程
- Servlet以及HttpServlet的源码分析
- STL学习笔记----15.STL算法之 (已序区间算法)
- C/C++经典面试题
- centreon+nagios安装测试-环境搭建
- Android 4.0+ 版本中的EditText字符重叠问题
- NOPI导出excel 有下标上标设置
- Ext.onReady详解
- Maven入门--概念与实例
- c# 修改文本文件内容后并保存(转自百度知道)
- GetLastError 错误号意义!0-------999