针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序。
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。
- 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)
- 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)