将两个连续的范围的元素分为单个订单的范围,排序的标准可能是按二进制谓词指定。
template<class BidirectionalIterator>
void inplace_merge(
BidirectionalIterator _First,
BidirectionalIterator _Middle,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class Predicate>
void inplace_merge(
BidirectionalIterator _First,
BidirectionalIterator _Middle,
BidirectionalIterator _Last,
Predicate _Comp
);
参数
_First
处理第一元素位置的双向迭代器在第一运行排序的范围将和排序为一个范围。_Middle
处理第一元素位置的双向迭代器位于第二两个连续的范围中合并和排序为一个范围。_Last
寻址个元素都具有双向迭代器位置的一个第二两个连续的范围中合并和排序为一个范围。_Comp
用户定义的谓词函数对象定义一个元素大于另一个。 二进制谓词采用两个参数,并且在满足第一个元素小于第二个元素时返回 true,在未满足时返回 false。
备注
引用的顺序连续的范围必须是有效的;所有指针必须 dereferenceable,因此,在每个序列中,最后位置必须是可访问的。从开始递增。
必须将每一个的顺序连续的范围,当给 inplace_merge 算法的应用程序的一个前置条件。的顺序相同。将算法使用顺序组合的范围。 因为元素相对顺序在每个范围内保留的操作,是稳定的。 当在两个源区时的等效元素,元素为第一范围位于从第二个元素。组合的范围。
在算法将内存分配到临时缓冲区,复杂取决于可用内存。 如果有足够的可用内存,最好的条件是线性的。(_Last – _First) – 1 比较;如果附加内存不可用,最坏的情况为 N log*(N),其中 N = (_Last – _First*)。
示例
// alg_inplace_merge.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> //For greater<int>( )
#include <iostream>
// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
}
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1, Iter2, Iter3;
// Constructing vector v1 with default less-than ordering
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( i );
}
int ii;
for ( ii =-5 ; ii <= 0 ; ii++ )
{
v1.push_back( ii );
}
cout << "Original vector v1 with subranges sorted by the\n "
<< "binary predicate less than is v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// Constructing vector v2 with ranges sorted by greater
vector <int> v2 ( v1 );
vector <int>::iterator break2;
break2 = find ( v2.begin ( ) , v2.end ( ) , -5 );
sort ( v2.begin ( ) , break2 , greater<int> ( ) );
sort ( break2 , v2.end ( ) , greater<int> ( ) );
cout << "Original vector v2 with subranges sorted by the\n "
<< "binary predicate greater is v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// Constructing vector v3 with ranges sorted by mod_lesser
vector <int> v3 ( v1 );
vector <int>::iterator break3;
break3 = find ( v3.begin ( ) , v3.end ( ) , -5 );
sort ( v3.begin ( ) , break3 , mod_lesser );
sort ( break3 , v3.end ( ) , mod_lesser );
cout << "Original vector v3 with subranges sorted by the\n "
<< "binary predicate mod_lesser is v3 = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")" << endl;
vector <int>::iterator break1;
break1 = find (v1.begin ( ) , v1.end ( ) , -5 );
inplace_merge ( v1.begin( ), break1, v1.end( ) );
cout << "Merged inplace with default order,\n vector v1mod = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To merge inplace in descending order, specify binary
// predicate greater<int>( )
inplace_merge ( v2.begin( ), break2 , v2.end( ) , greater<int>( ) );
cout << "Merged inplace with binary predicate greater specified,\n "
<< "vector v2mod = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")" << endl;
// Applying a user defined (UD) binary predicate mod_lesser
inplace_merge ( v3.begin( ), break3, v3.end( ), mod_lesser );
cout << "Merged inplace with binary predicate mod_lesser specified,\n "
<< "vector v3mod = ( " ; ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")" << endl;
}
要求
标头: <算法>
命名空间: std