已序区间算法

针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序。

1.搜寻元素

检查某个元素是否存在

  • bool binary_search( ForwardIterator beg, ForwardIterator end, const T& value) //判断已序区间[beg, end)中是否包含"和value等值"的元素。存在返回true。
  • bool binary_search( ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)
检查若干值是否存在

  • bool includes( InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd) //判断已序区间[beg, end)是否包含另一个[searchBeg, searchEnd)中的全部元素。
  • bool includes( InputIterator1 beg, InputIterator1 end, InputIterator2 searchBeg, InputIterator2 searchEnd, BinaryPredicate op)
搜寻第一个或最后一个可能位置

  • ForwardIterator lower_bound( ForwardIterator beg, ForwardIterator end, const T& value) //返回第一个"大于等于value"的元素位置。可插入的第一个位置。
  • ForwardIterator lower_bound( ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)
  • ForwardIterator upper_bound( ForwardIterator beg, ForwardIterator end, const T& value) //返回第一个"大于value"的元素位置。可插入的最后一个位置。
  • ForwardIterator upper_bound( ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)
  • 注:如要同时获得lower_bound()和upper_bound()的结果,使用equal_range()
  • 关联式容器有等效的成员函数。
搜寻第一个和最后一个可能位置

  • pair<ForwardIterator, ForwardIterator> equal_range( ForwardIterator beg, ForwardIterator end, const T& value) //与make_pari(lower_bound(),upper_bound())等效。
  • pair<ForwardIterator, ForwardIterator> equal_range( ForwardIterator beg, ForwardIterator end, const T& value, BinaryPredicate op)
  • 注:关联式容器有自己的等效成员函数

2.合并元素

两个已序集合的总和(Sum)

  • OutputIterator merge( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg) //将[source1Beg, source1End)和[source2Beg, source2End)内的元素合并,使destBeg含有两个源区间的所有元素。
  • OutputIterator merge( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
  • 注:目标区间和源区间不得重复。list有特殊的成员函数merge()来合并2个lists。
两个已序结合的并集(Union)
  • OutputIterator set_union( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg) //将已序[source1Beg, source1End) 和[source2Beg, source2End)内的元素合并,得到以destBeg开始的目标区间,目标区间的元素要不来自第一源区间,要不来自第二源区间,或是同时来自两个源区间。返回目标区间第一个未覆盖的元素位置。
  • OutputIterator set_union( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
两个已序集合的交集(Intersection)
  • OutputIterator set_intersection( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg) //目标区间的元素不但存在于第一源区间,也存在于第二源区间。
  • OutputIterator set_intersection( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
两个已序集合的差集(Difference)
  • OutputIterator set_differcece( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg) //目标区间的元素只存在于第一源区间,不存在第二源区间。
  • OutputIterator set_differcece( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
两个已序集合的异或
  • OutputIterator set_symmetric_differcece( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg) //目标区间的元素或存在于第一源区间,或存在第二源区间,但不同时存在两个区间
  • OutputIterator set_symmetric_differcece( InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg, BinaryPredicate op)
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int c1[]={1,2,2,4,6,7,7,9};
	int num1= sizeof(c1)/sizeof(int);
	int c2[]={2,2,2,3,6,6,8,9};
	int num2= sizeof(c2)/sizeof(int);
	cout<<"c1: ";
	copy(c1,c1+num1,ostream_iterator<int>(cout," "));
	cout<<endl;
	cout<<"c2: ";
	copy(c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<"\n"<<endl;

	cout<<"merge():";
	merge(c1,c1+num1,c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<endl;

	cout<<"set_union():";
	set_union(c1,c1+num1,c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<endl;

	cout<<"set_intersection():";
	set_intersection(c1,c1+num1,c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<endl;

	cout<<"set_difference(): ";
	set_difference(c1,c1+num1,c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<endl;

	cout<<"set_symmetric_difference():";
	set_symmetric_difference(c1,c1+num1,c2,c2+num2,ostream_iterator<int>(cout," "));
	cout<<endl;
	return 0;
}


将连贯的已序区间合并
  • void inplace_merge( BidirectionalInputIterator beg1, BidirectionalInputIterator end1beg2, BidirectionalInputIterator end2) //将[beg1,end1beg2)和[end1beg2,end2)的元素合并
  • void inplace_merge( BidirectionalInputIterator beg1, BidirectionalInputIterator end1beg2, BidirectionalInputIterator end2, BidirectionalInputIterator op)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值