Espacios de nombres
Variantes
Acciones

std::is_sorted_until

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)
is_sorted_until
(C++11)
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 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,

                           Compare comp );
(2) (desde C++11)
Examina la [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 .
Original:
Examines the range [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.
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 -
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 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 ForwardIterator puede ser desreferenciado y luego convertido implícitamente a ambos. ​

Requisitos de tipo
-
ForwardIt debe reunir los requerimientos de ForwardIterator.

[editar] Valor de retorno

El límite superior de la gama más amplia en principio 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 .
Original:
The upper bound of the largest range beginning at first in which the elements are sorted in ascending order. That is, the last iterator it for which range [first, it) is sorted.
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 la distancia entre first y last
Original:
linear in the distance between first and last
The text has been machine-translated via Google Translate.
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) [editar]