您的位置:首页 > 其它

STL学习笔记----15.STL算法之 (已序区间算法)

2013-10-17 14:41 447 查看
一. 概述

都是针对已序区间执行的算法。


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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: