inplace_merge
合併兩個連續的已排序範圍內的項目結合成單一排序的範圍,排序準則可能是由二進位述詞指定。
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
解決雙向 Iterator 的第一個項目的位置在第一個或兩個連續排序範圍會結合和排序到單一範圍。_Middle
解決雙向 Iterator 的第一個項目的位置在第二個兩個連續的已排序的範圍中合併和排序成單一範圍。_Last
解決一的雙向 Iterator 超過最後一個項目的位置是在第二個兩個連續的已排序的範圍中合併和排序成單一範圍。_Comp
定義感受遠遠之使用者定義的述詞函式物件哪個項目大於另一個執行個體。 這個二進位述詞會採用兩個引數,而且應傳回 true ,當第一個項目小於則為第二個項目和 false 小於時。
備註
參考的已排序之連續範圍必須是有效的,任何指標必須 dereferenceable,必要時,在每個序列中,最後一個位置必須是可取得的開頭會增加。
必須將每個已排序的連續範圍,則為 inplace_merge 演算法的應用程式的前提是與排程相同符合與將演算法使用排序合併的範圍。 ,其項目相對順序在每個範圍中,儲存作業是否穩定。 當有兩個來源範圍時的對等項目,是第一個範圍在第二個插入的項目之前在此合併的範圍。
,當演算法配置記憶體至暫存緩衝區,複雜依可用的記憶體。 如果沒有足夠的記憶體可用,這是最理想的情況是線性 (_Last – _First– 1) 比較,如果輔助記憶體不可用,最壞的情況是 N 記錄*(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;
}
需求
標題: <algorithm>
命名空間: std