Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library Reference Guide

nth_element()

Library:  Algorithms


Function

Local Index

No Entries

Summary

An algorithm that rearranges a collection so that all elements lower in sorted order than the nth element come before it, and all elements higher in sorted order than the nth element come after it

Synopsis

#include <algorithm>

namespace std {
  template <class RandomAccessIterator>
  void nth_element(RandomAccessIterator start,
                   RandomAccessIterator nth,
                   RandomAccessIterator finish);

  template <class RandomAccessIterator, class Compare>
  void nth_element(RandomAccessIterator start,
                   RandomAccessIterator nth,
                   RandomAccessIterator finish,
                   Compare comp);
}

Description

The nth_element() algorithm rearranges a collection according to either operator<() or the function object comp. After the algorithm is applied, the follwoing hold:

That is, for any iterator i in the range [start, nth) and any iterator j in the range [nth, finish), it holds that !(*i > *j) or comp(*i, *j) == false.

Note that the elements that precede or follow the nth position are not necessarily sorted relative to each other. The nth_element() algorithm does not sort the entire collection.

Complexity

The algorithm is linear, on average, where N is the size of the range [start, finish).

Example

See Also

Algorithms

Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.2



Previous fileTop of DocumentContentsIndex pageNext file