Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Standard C++ Library Module Reference Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

nth_element()

Module:  Standard C++ Library   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

Copyright (c) 1994-2006 Rogue Wave Software, a Quovadx Division.
Licensed under the Apache License, Version 2.0.
Contact Rogue Wave about documentation or support issues. You can also seek help from other developers through the Apache stdcxx community (see below).

For more information on the Rogue Wave Standard C++ Library under open source, see Section 1.2 of the user's guide.