STL学习笔记----15.STL算法之 (已序区间算法)
2013-10-17 14:41
447 查看
一. 概述
都是针对已序区间执行的算法。
二. 搜索元素
1. 检查某个元素是否存在
[cpp]
view plaincopyprint?
//判断已序区间[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)
2. 检查若干个值是否存在
[cpp]
view plaincopyprint?
//判断已序区间[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)
3. 搜索第一个或最后一个可能位置
[cpp]
view plaincopyprint?
//返回第一个"大于等于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)
4. 搜索第一个和最后一个可能位置
[cpp]
view plaincopyprint?
//返回"与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)
[cpp]
view plaincopyprint?
//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)
2.. 两个已序集合的并集(Union)
[cpp]
view plaincopyprint?
//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)
3. 两个已序集合的交集(Intersection)
[cpp]
view plaincopyprint?
//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)
4. 两个已序集合的差集(Difference)
[cpp]
view plaincopyprint?
//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)
5. 将连贯的已序区间合并
[cpp]
view plaincopyprint?
//将已序[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)
都是针对已序区间执行的算法。
binary_search() | 判断某区间内是否包含某个元素 |
includes() | 判断某区间内的每一个元素是否都涵盖于另一区间中 |
lower_bound() | 搜索第一个"大于等于给定值"的元素 |
upper _bound() | 搜索第一个"大于给定值"的元素 |
equal_range() | 返回"等于给定值"的所有元素构成的区间 |
merge() | 将两个区间合并 |
set_union() | 求两个区间的并集 |
set_intersection() | 求两个区间的交集 |
set_difference() | 求位于第一区间但不位于第二区间的所有元素,形成一个已序区间 |
set_symmetric_difference() | 找出只出现于两区间之一的所有元素,形成一个已序区间 |
inplace_merge() | 将两个连续的已序区间合并 |
1. 检查某个元素是否存在
[cpp]
view plaincopyprint?
//判断已序区间[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等值的元素 bool binary_search (ForwardIterator beg, ForwardIterator end, const T& value) bool binary_search (ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)
2. 检查若干个值是否存在
[cpp]
view plaincopyprint?
//判断已序区间[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) bool includes (InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd) bool includes (InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd, BinaryPredicate op)
3. 搜索第一个或最后一个可能位置
[cpp]
view plaincopyprint?
//返回第一个"大于等于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"的元素位置,如果不存在返回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)
4. 搜索第一个和最后一个可能位置
[cpp]
view plaincopyprint?
//返回"与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)
[cpp]
view plaincopyprint?
//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为起点输出 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)
2.. 两个已序集合的并集(Union)
[cpp]
view plaincopyprint?
//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 //这个区间内含的元素要不来自第一源区间,要不来自第二源区间 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)
3. 两个已序集合的交集(Intersection)
[cpp]
view plaincopyprint?
//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 //元素必须同时在两个区间 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)
4. 两个已序集合的差集(Difference)
[cpp]
view plaincopyprint?
//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 //只在第一个区间,不在第二个区间 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)
5. 将连贯的已序区间合并
[cpp]
view plaincopyprint?
//将已序[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)成为两者之和 void inplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2, BidirectionalIterator end2) void inplace_merge (BidirectionalIterator beg1, BidirectionalIterator end1beg2, BidirectionalIterator end2, BinaryPredicate op)
相关文章推荐
- STL学习笔记----15.STL算法之 (已序区间算法)
- C++ Primer 学习笔记_45_STL实践与分析(19)--建筑常规算法
- 【算法学习笔记】37.区间合并问题 SJTU OJ 1262 Milking Cow
- STL学习笔记----10.STL算法之 (非变动性算法)
- STL学习笔记----11.STL算法之 (变动性算法)
- STL学习笔记----9.STL算法之 for_each()
- 泛型编程与STL学习笔记之算法
- C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】
- 【算法学习笔记】57. 前缀树 字典序优化技巧 STL学习 SJTU OJ 1366 前缀匹配
- STL学习笔记----10.STL算法之 (非变动性算法)
- 【算法学习笔记】51. 贪心法 区间排序问题 SJTU OJ 1360 偶像丁姐的烦恼
- STL学习笔记----16.STL算法之 (数值算法)
- C++精华(STL)学习笔记之算法
- STL学习笔记--数值算法
- STL学习笔记之sort算法
- STL学习笔记----13.STL算法之 (变序性算法)
- C++ Primer Plus学习笔记之STL算法
- SGI STL学习笔记(3):copy算法实现细节
- C++精华(STL)学习笔记之算法
- STL的内观排序(introsort)算法学习笔记