Espacios de nombres
Variantes
Acciones

std::nth_element

De cppreference.com
< cpp‎ | algorithm
 
 
Biblioteca de algoritmos
Políticas de ejecución (C++17)
Operaciones de secuencia no modificantes
(C++11)(C++11)(C++11)
(C++17)
Operaciones de secuencia modificantes
Operaciones en almacenamiento no inicializado
Operaciones de partición
Operaciones de ordenación
(C++11)
nth_element

Operaciones de búsqueda binaria
Operaciones de conjuntos (en rangos ordenados)
Operaciones de pila
(C++11)
Operaciones mínimo/máximo
(C++11)
(C++17)
Permutaciones
Operaciones numéricas
Bibliotecas C
 
Definido en el archivo de encabezado <algorithm>
template< class RandomIt >
void nth_element( RandomIt first, RandomIt nth, RandomIt last );
(1)
template< class RandomIt, class Compare >
void nth_element( RandomIt first, RandomIt nth, RandomIt last, Compare comp );
(2)
Parcialmente el tipo [first, last) rango en orden ascendente de manera que todos los elementos en el intervalo [first, nth) son' menos que aquellos en el rango [nth, last). La primera versión utiliza operator< para comparar los elementos, la segunda versión utiliza la comparación comp función dada. El elemento colocado en la posición nth es exactamente el elemento que se producirían en esta posición si el rango se solucionó completamente .
Original:
Partially sorts the range [first, last) in ascending order so that all elements in the range [first, nth) are less than those in the range [nth, last). The first version uses operator< to compare the elements, the second version uses the given comparison function comp. The element placed in the nth position is exactly the element that would occur in this position if the range was fully sorted.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar] Parámetros

first, last -
iteradores de acceso aleatorio que definen el tipo gama
Original:
random access iterators defining the range sort
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
nth -
iterador de acceso aleatorio que determina el punto de partición tipo
Original:
random access iterator defining the sort partition point
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
comp - objeto función de comparación (es decir, un objeto que satisface los requerimientos de Compare) que devuelve ​true si el primer argumento es menor que el segundo.

La signatura de la función de comparación deberá ser equivalente a lo siguiente:

 bool cmp(const Type1 &a, const Type2 &b);

Mientras que la signatura no necesita ser const &, la función no debe modificar los objetos que se le pasaron y debe admitir todos los valores de los tipos (posiblemente const) Type1 y Type2 a pesar de la categoría de valor (por consiguiente, no se permite a Type1 & , ni tampoco a Type1 a menos que para Type1 un movimiento sea equivalente a una copia (desde C++11)).
Los tipos Type1 y Type2 deben ser tales que un objeto de tipo RandomIt puede ser desreferenciado y luego convertido implícitamente a ambos. ​

Requisitos de tipo
-
RandomIt debe reunir los requerimientos de ValueSwappable y RandomAccessIterator.
-
The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible.

[editar] Valor de retorno

(Ninguno)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Complejidad

Lineal en std::distance(first, last) en promedio .
Original:
Linear in std::distance(first, last) on average.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Ejemplo

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> v{5, 6, 4, 3, 2, 6, 7, 9, 3};
 
    std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());
    std::cout << "The median is " << v[v.size()/2] << '\n';
 
    std::nth_element(v.begin(), v.begin()+1, v.end(), std::greater<int>());
    std::cout << "The second largest element is " << v[1] << '\n';
}

Salida:

The median is 5
The second largest element is 7

[editar] Ver también

Copia y ordena parcialmente un rango de elementos.
(plantilla de función) [editar]
Ordena un intervalo de elementos, mientras que la preservación del orden entre los elementos iguales
Original:
sorts a range of elements while preserving order between equal elements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(plantilla de función) [editar]
Ordena un intervalo en orden ascendente
Original:
sorts a range into ascending order
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(plantilla de función) [editar]