C++算法nth_element()函數用於將第一個和第n個元素之間的元素按升序排序,第n個和最後一個元素之間的元素不排序。但是,第 n 個和最後一個之間的元素不小於第一個和第 n 個元素之間的元素。
第一個版本使用運算符比較元素,第二個版本使用 comp 進行比較。
用法
default (1) template <class RandomAccessIterator>
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last);
custom (2) template <class RandomAccessIterator, class Compare>
void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last, Compare comp);
參數
first:指向要使用的範圍中的第一個元素的隨機訪問迭代器。
last:一個隨機訪問迭代器,指向要使用的範圍中過去的最後一個元素。
comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。
nth:一個隨機訪問迭代器,尋址範圍 [first, last) 中包含排序元素的位置。
返回值
空
複雜度
平均而言,複雜性在第一個和最後一個之間的距離上是線性的:比較元素並可能交換它們,直到元素被正確重新排列。
數據競爭
範圍 [first, last) 中的對象被更改。
異常
如果元素比較、元素交換或迭代器上的操作中的任何一個拋出異常,則此函數將拋出異常。
注意:無效的參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 nth_element() 的使用:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(vector<int> ar)
{
for(auto x:ar) cout << x << " ";
cout << endl;
}
int main()
{
vector<int> ar = {1, 3, 6, 1, 2, 4, 7, 0};
cout<<"Before:";
// will print 1 3 6 1 2 4 7 0
print(ar);
// mid = 5th element (ar.begin() + 4)
auto mid = ar.begin() + distance(ar.begin(), ar.end()) / 2;
// lets nth_element ar to mid
nth_element(ar.begin(), mid, ar.end());
cout<<"\nAfter:";
// will print 2 0 1 1 3 4 7 6
// mid points to element 3
print(ar);
return 0;
}
輸出:
Before:1 3 6 1 2 4 7 0 After:2 0 1 1 3 4 7 6
例子2
讓我們看另一個簡單的例子:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
vector<int> v{5, 6, 4, 3, 2, 6, 7, 9, 3};
cout<<"Elements are:";
for (vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
cout << ' ' << *it;
cout << '\n';
nth_element(v.begin(), v.begin() + v.size()/2, v.end());
cout << "The median is " << v[v.size()/2] << '\n';
nth_element(v.begin(), v.begin()+1, v.end(), greater<int>());
cout << "The second largest element is " << v[1] << '\n';
return 0;
}
輸出:
Elements are: 5 6 4 3 2 6 7 9 3 The median is 5 The second largest element is 7
例子3
讓我們看另一個簡單的例子:
#include <iostream> // std::cout
#include <algorithm> // std::nth_element, std::random_shuffle
#include <vector> // std::vector
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main () {
vector<int> myvector;
// set some values:
for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
random_shuffle (myvector.begin(), myvector.end());
// using default comparison (operator <):
nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
// print out content:
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
輸出:
myvector contains:5 2 3 1 4 6 7 8 9
示例 4
讓我們看另一個簡單的例子:
#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( 3 * i );
int ii;
for ( ii = 0 ; ii <= 5 ; ii++ )
v1.push_back( 3 * ii + 1 );
int iii;
for ( iii = 0 ; iii <= 5 ; iii++ )
v1.push_back( 3 * iii +2 );
cout << "Original vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
nth_element(v1.begin( ), v1.begin( ) + 3, v1.end( ) );
cout << "Position 3 partitioned vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
// To sort in descending order, specify binary predicate
nth_element( v1.begin( ), v1.begin( ) + 4, v1.end( ),
greater<int>( ) );
cout << "Position 4 partitioned (greater) vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
random_shuffle( v1.begin( ), v1.end( ) );
cout << "Shuffled 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
nth_element( v1.begin( ), v1.begin( ) + 5, v1.end( ), UDgreater );
cout << "Position 5 partitioned (UDgreater) vector:\n v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
return 0;
}
輸出:
Original vector: v1 = ( 0 3 6 9 12 15 1 4 7 10 13 16 2 5 8 11 14 17 ) Position 3 partitioned vector: v1 = ( 1 0 2 3 8 5 9 4 7 6 10 16 13 15 12 11 14 17 ) Position 4 partitioned (greater) vector: v1 = ( 16 17 14 15 13 12 11 9 7 8 10 6 1 4 5 3 2 0 ) Shuffled vector: v1 = ( 13 10 6 3 5 2 0 17 11 8 15 9 7 14 16 1 12 4 ) Position 5 partitioned (UDgreater) vector: v1 = ( 14 17 15 16 13 12 10 11 9 8 0 2 7 5 3 1 6 4 )
相關用法
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
- C++ Algorithm transform()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
- C++ Algorithm is_sorted()用法及代碼示例
- C++ Algorithm equal_range()用法及代碼示例
- C++ Algorithm minmax_element()用法及代碼示例
- C++ Algorithm stable_sort()用法及代碼示例
- C++ Algorithm min()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm nth_element()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。