STL's Nth_element () (takes the nth largest value in the container)

Source: Internet
Author: User

Nth_element () function

Header files: #include <algorithm>

Function: Thenth_element function is to find the nth element, and put it in the nth position, subscript is starting from 0 counting, that is, the No. 0 small element is the smallest number.

such as: A[start,end] element interval. After sorting a[n] is the number of n+1 in the series (the subscript starts from 0 count). To be aware of is a[start,n),

The order of size within a[n,end] is not necessarily.

Can only be determined a[n] is the number of n+1 in the series.

Of course a[start,n) in the number certainly not greater than

The number in A[n,end].

Note: the Nth_element () function does not return a value because it is in the position of the nth large number.

The instance code is as follows:

#include <iostream> #include <algorithm>using namespace std; the int main () {int a[]={1,3,4,5,2,6,8,7,9};int i;cout<< "sequence is as follows:" <<endl;for (i=0;i<9;i++)   cout< <a[i]<< ""; Nth_element (a,a+5,a+9);    cout<<endl<< "The fifth largest number of outputs:" <<a[4]<<endl; Note that the subscript is a return of 0 from the beginning of the 0 count;}


STL's Nth_element () (takes the nth largest value in the container)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.