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

来源:互联网 发布:内网映射软件 编辑:程序博客网 时间:2024/06/05 03:18

一. 概述

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

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?
  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等值的元素boolbinary_search (ForwardIterator beg, ForwardIterator end,                const T& value)boolbinary_search (ForwardIterator beg, ForwardIterator end,                const T& value,                BinaryPredicate op)
2. 检查若干个值是否存在

[cpp] view plaincopyprint?
  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)boolincludes (InputIterator1 beg, InputIterator1 end,           InputIterator2 searchBeg, InputIterator2 searchEnd)boolincludes (InputIterator1 beg, InputIterator1 end,           InputIterator2 searchBeg, InputIterator2 searchEnd,           BinaryPredicate op)
3. 搜索第一个或最后一个可能位置
[cpp] view plaincopyprint?
  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"的元素位置,如果不存在返回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. 搜索第一个和最后一个可能位置

[cpp] view plaincopyprint?
  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)

[cpp] view plaincopyprint?
  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为起点输出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)

[cpp] view plaincopyprint?
  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//这个区间内含的元素要不来自第一源区间,要不来自第二源区间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)

[cpp] view plaincopyprint?
  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//元素必须同时在两个区间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)

[cpp] view plaincopyprint?
  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//只在第一个区间,不在第二个区间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. 将连贯的已序区间合并

[cpp] view plaincopyprint?
  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)成为两者之和voidinplace_merge (BidirectionalIterator beg1,                BidirectionalIterator end1beg2,                BidirectionalIterator end2)voidinplace_merge (BidirectionalIterator beg1,                BidirectionalIterator end1beg2,                BidirectionalIterator end2,                BinaryPredicate op)

原创粉丝点击