partial_sort
具有較小的項目的指定數目範圍內的輸入 nondescending 的命令或以二進位述詞指定的排序準則。
template<class RandomAccessIterator>
void partial_sort(
RandomAccessIterator first,
RandomAccessIterator sortEnd,
RandomAccessIterator last
);
template<class RandomAccessIterator, class BinaryPredicate>
void partial_sort(
RandomAccessIterator first,
RandomAccessIterator sortEnd,
RandomAccessIterator last
BinaryPredicate comp
);
參數
first
解決的隨機存取 Iterator 的第一個項目位置在要排序的範圍。sortEnd
解決的隨機存取 Iterator 超過最後一個項目的位置是在要排序的子範圍。last
解決的隨機存取 Iterator 超過最後一個項目的位置是在部分已排序的範圍。comp
定義連續項目會滿足比較準則順序的使用者定義之述詞函式物件。 一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。
備註
參考的範圍必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。
如果兩者都比其他,不小於項目相等,則為,但不一定會等於。 sort 演算法不穩定,而且不保證相對排程對等項目來儲存。 這個演算法 stable_sort 儲存原始順序。
平均部分排序複雜度是 O(last(-)first記錄 (sortEnd-first))。
範例
// alg_partial_sort.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream>
// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 )
{
return elem1 > elem2;
}
int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
int i;
for ( i = 0 ; i <= 5 ; i++ )
{
v1.push_back( 2 * i );
}
int ii;
for ( ii = 0 ; ii <= 5 ; ii++ )
{
v1.push_back( 2 * ii +1 );
}
cout << "Original vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
partial_sort(v1.begin( ), v1.begin( ) + 6, v1.end( ) );
cout << "Partially sorted vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To partially sort in descending order, specify binary predicate
partial_sort(v1.begin( ), v1.begin( ) + 4, v1.end( ), greater<int>( ) );
cout << "Partially resorted (greater) vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// A user-defined (UD) binary predicate can also be used
partial_sort(v1.begin( ), v1.begin( ) + 8, v1.end( ),
UDgreater );
cout << "Partially resorted (UDgreater) vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
}
需求
標題: <algorithm>
命名空間: std