std::is_sorted_until
![]() |
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 ForwardIt > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(1) | (desde C++11) |
template< class ForwardIt, class Compare > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, |
(2) | (desde C++11) |
[first, last)
rango y encuentra la más amplia gama en principio first
en el que los elementos se ordenan en orden ascendente. La primera versión de la función utiliza operator< para comparar los elementos, el segundo utiliza la función de comparación dado comp
.[first, last)
and finds the largest range beginning at first
in which the elements are sorted in ascending order. The first version of the function uses operator< to compare the elements, the second uses the given comparison function comp
.You can help to correct and verify the translation. Click here for instructions.
Contenido |
[editar] Parámetros
first, last | - | la gama de elementos a examinar
Original: the range of elements to examine 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 | ||
-ForwardIt debe reunir los requerimientos de ForwardIterator .
|
[editar] Valor de retorno
first
en el que los elementos se ordenan en orden ascendente. Es decir, el iterador it
último para el que se ordena [first, it)
rango .first
in which the elements are sorted in ascending order. That is, the last iterator it
for which range [first, it)
is sorted.You can help to correct and verify the translation. Click here for instructions.
[editar] Complejidad
first
y last
first
and last
You can help to correct and verify the translation. Click here for instructions.
[editar] Posible implementación
Primera versión |
---|
template<class ForwardIt> ForwardIt is_sorted_until(ForwardIt first, ForwardIt last) { if (first != last) { ForwardIt next = first; while (++next != last) { if (*next < *first) return next; first = next; } } return last; } |
Segunda versión |
template< class ForwardIt, class Compare > ForwardIt is_sorted_until(ForwardIt first, ForwardIt last, Compare comp) { if (first != last) { ForwardIt next = first; while (++next != last) { if (comp(*next, *first)) return next; first = next; } } return last; |
[editar] Ejemplo
#include <iostream> #include <algorithm> #include <iterator> #include <random> int main() { std::random_device rd; std::mt19937 g(rd()); const int N = 6; int nums[N] = {3, 1, 4, 1, 5, 9}; const int min_sorted_size = 4; int sorted_size = 0; do { std::random_shuffle(nums, nums + N, g); int *sorted_end = std::is_sorted_until(nums, nums + N); sorted_size = std::distance(nums, sorted_end); for (auto i : nums) std::cout << i << ' '; std::cout << " : " << sorted_size << " initial sorted elements\n"; } while (sorted_size < min_sorted_size); }
Posible salida:
4 1 9 5 1 3 : 1 initial sorted elements 4 5 9 3 1 1 : 3 initial sorted elements 9 3 1 4 5 1 : 1 initial sorted elements 1 3 5 4 1 9 : 3 initial sorted elements 5 9 1 1 3 4 : 2 initial sorted elements 4 9 1 5 1 3 : 2 initial sorted elements 1 1 4 9 5 3 : 4 initial sorted elements
[editar] Ver también
(C++11) |
Comprueba si un rango se clasifican en orden ascendente Original: checks whether a range is sorted 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) |