std::nth_element
![]() |
Esta página se ha traducido por ordenador/computador/computadora de la versión en inglés de la Wiki usando Google Translate.
La traducción puede contener errores y palabras aparatosas/incorrectas. Planea sobre el texto para ver la versión original. Puedes ayudar a corregir los errores y mejorar la traducción. Para instrucciones haz clic aquí. |
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) | |
[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 .[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.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 |
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
You can help to correct and verify the translation. Click here for instructions.
[editar] Complejidad
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) | |
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) | |
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) |