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

本文深入探讨了区间操作中的关键算法,包括查找、合并、并集、交集、差集等,提供了实例代码及详细解释,旨在帮助开发者理解和应用这些算法解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 概述

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

binary_search()
判断某区间内是否包含某个元素
includes()判断某区间内的每一个元素是否都涵盖于另一区间中
lower_bound()搜索第一个"大于等于给定值"的元素
upper _bound()搜索第一个"大于给定值"的元素
equal_range()返回"等于给定值"的所有元素构成的区间
merge()将两个区间合并
set_union()求两个区间的并集
set_intersection()求两个区间的交集
set_difference()求位于第一区间但不位于第二区间的所有元素,形成一个已序区间
set_symmetric_difference()找出只出现于两区间之一的所有元素,形成一个已序区间
inplace_merge()将两个连续的已序区间合并
二. 搜索元素

1. 检查某个元素是否存在

  1. //判断已序区间[beg, end)是否包含和value等值的元素 
  2. bool 
  3. binary_search (ForwardIterator beg, ForwardIterator end,  
  4.                const T& value) 
  5.  
  6. bool 
  7. binary_search (ForwardIterator beg, ForwardIterator end,  
  8.                const T& value,  
  9.                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. 检查若干个值是否存在

  1. //判断已序区间[beg, end)是否包含另一个已序区间[searchBeg, searchEnd) 
  2. bool 
  3. includes (InputIterator1 beg, InputIterator1 end,  
  4.           InputIterator2 searchBeg, InputIterator2 searchEnd) 
  5.  
  6. bool 
  7. includes (InputIterator1 beg, InputIterator1 end,  
  8.           InputIterator2 searchBeg, InputIterator2 searchEnd,  
  9.           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. 搜索第一个或最后一个可能位置
  1. //返回第一个"大于等于value"的元素位置,如果不存在返回end 
  2. ForwardIterator 
  3. lower_bound (ForwardIterator beg, ForwardIterator end,  
  4.              const T& value) 
  5.  
  6. ForwardIterator 
  7. lower_bound (ForwardIterator beg, ForwardIterator end,  
  8.              const T& value,  
  9.              BinaryPredicate op) 
  10.  
  11. //返回第一个"大于value"的元素位置 
  12. ForwardIterator 
  13. upper_bound (ForwardIterator beg, ForwardIterator end,  
  14.              const T& value) 
  15.  
  16. ForwardIterator 
  17. upper_bound (ForwardIterator beg, ForwardIterator end,  
  18.              const T& value,  
  19.              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. 搜索第一个和最后一个可能位置

  1. //返回"与value相等"的元素所形成的区间 
  2. pair<ForwardIterator,ForwardIterator> 
  3. equal_range (ForwardIterator beg, ForwardIterator end,  
  4.              const T& value) 
  5.  
  6. pair<ForwardIterator,ForwardIterator> 
  7. equal_range (ForwardIterator beg, ForwardIterator end,  
  8.              const T& value,  
  9.              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)

  1. //source1: 1 2 2 4 6 7 7 9 
  2. //source2: 2 2 2 3 6 6 8 9 
  3. //Sum:     1 2 2 2 2 2 3 4 6 6 6 7 7 8 9 9 
  4. //将源区间[source1Beg, source1End)和[source2Beg, source2End)内的元素合并 
  5. //以destBeg为起点输出 
  6. OutputIterator 
  7. merge (InputIterator source1Beg, InputIterator source1End,  
  8.        InputIterator source2Beg, InputIterator source2End,  
  9.        Output Iterator destBeg) 
  10.  
  11. OutputIterator 
  12. merge (InputIterator source1Beg, InputIterator source1End,  
  13.        InputIterator source2Beg, InputIterator source2End,  
  14.        OutputIterator destBeg,  
  15.        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)

  1. //source1: 1 2 2 4 6 7 7 9 
  2. //source2: 2 2 2 3 6 6 8 9 
  3. //Union:   1 2 2 2 3 4 6 6 7 7 8 9 
  4. //这个区间内含的元素要不来自第一源区间,要不来自第二源区间 
  5. OutputIterator 
  6. set_union (InputIterator source1Beg, InputIterator source1End,  
  7.            InputIterator source2Beg, InputIterator source2End,  
  8.            OutputIterator destBeg) 
  9.  
  10. OutputIterator 
  11. set_union (InputIterator source1Beg, InputIterator source1End,  
  12.            InputIterator source2Beg, InputIterator source2End,  
  13.            OutputIterator destBeg,  
  14.            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)

  1. //source1: 1 2 2 4 6 7 7 9 
  2. //source2: 2 2 2 3 6 6 8 9 
  3. //Inter:   2 2 6 9 
  4. //元素必须同时在两个区间 
  5. OutputIterator 
  6. set_intersection (InputIterator source1Beg, InputIterator source1End,  
  7.                   InputIterator source2Beg, InputIterator source2End,  
  8.                   OutputIterator destBeg) 
  9.  
  10. OutputIterator 
  11. set_intersection (InputIterator source1Beg, InputIterator source1End,  
  12.                   InputIterator source2Beg, InputIterator sotirce2End,  
  13.                   OutputIterator destBeg,  
  14.                   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)

  1. //source1: 1 2 2 4 6 7 7 9 
  2. //source2: 2 2 2 3 6 6 8 9 
  3. //differ:  1 4 7 7 
  4. //只在第一个区间,不在第二个区间 
  5. OutputIterator 
  6. set_difference (InputIterator source1Beg, InputIterator source1End,  
  7.                 InputIterator source2Beg, InputIterator source2End,  
  8.                 OutputIterator destBeg) 
  9.  
  10. OutputIterator 
  11. set_difference (InputIterator source1Beg, InputIterator source1End,  
  12.                 InputIterator source2Beg, InputIterator source2End,  
  13.                 OutputIterator destBeg,  
  14.                 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. 将连贯的已序区间合并

  1. //将已序[beg1, end1beg2)和[end1beg2, end2)的元素合并, 
  2. //使区间[beg1, end2)成为两者之和 
  3. void 
  4. inplace_merge (BidirectionalIterator beg1,  
  5.                BidirectionalIterator end1beg2,  
  6.                BidirectionalIterator end2) 
  7.  
  8. void 
  9. inplace_merge (BidirectionalIterator beg1,  
  10.                BidirectionalIterator end1beg2,  
  11.                BidirectionalIterator end2,  
  12.                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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值