Espacios de nombres
Variantes
Acciones

std::inplace_merge

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)
Operaciones de búsqueda binaria
Operaciones de conjuntos (en rangos ordenados)
inplace_merge
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 BidirIt >
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last );
(1)
template< class BidirIt, class Compare>
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp );
(2)
Combina dos cadenas consecutivas ordenados [first, middle) y [middle, last) en un rango [first, last) ordenada. El orden de los elementos iguales se garantiza que se conservan. La primera versión utiliza operator< para comparar los elementos, la segunda versión utiliza la función de comparación dado comp .
Original:
Merges two consecutive sorted ranges [first, middle) and [middle, last) into one sorted range [first, last). The order of equal elements is guaranteed to be preserved. The first version uses operator< to compare the elements, the second version 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 -
el comienzo de la primera gama ordenados
Original:
the beginning of the first sorted range
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
middle -
el final de la primera gama ordenados y el comienzo de la segunda
Original:
the end of the first sorted range and the beginning of the second
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
last -
el final de la segunda gama ordenados
Original:
the end of the second sorted range
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 BidirIt puede ser desreferenciado y luego convertido implícitamente a ambos. ​

Requisitos de tipo
-
BidirIt debe reunir los requerimientos de ValueSwappable y BidirectionalIterator.
-
The type of dereferenced BidirIt 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

Exactamente N-1 comparaciones memoria adicional si se dispone de suficiente, de lo contrario N·log(N) donde N = std::distance(first, last) .
Original:
Exactly N-1 comparisons if enough additional memory is available, otherwise N·log(N) where N = std::distance(first, last).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Notas

Esta función intenta asignar un búfer temporal, normalmente llamando std::get_temporary_buffer. Si la asignación falla, el algoritmo menos eficiente es elegido .
Original:
This function attempts to allocate a temporary buffer, typically by calling std::get_temporary_buffer. If the allocation fails, the less efficient algorithm is chosen.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Ejemplo

El código siguiente es una implementación del tipo de mezcla .
Original:
The following code is an implementation of merge sort.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
#include <iostream>
#include <algorithm>
 
template<class Iter>
void merge_sort(Iter first, Iter last)
{
    if (last - first > 1) {
        Iter middle = first + (last - first) / 2;
        merge_sort(first, middle);
        merge_sort(middle, last);
        std::inplace_merge(first, middle, last);
    }
}
 
int main()
{
    std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3};
    merge_sort(v.begin(), v.end());
    for(auto n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
}

Salida:

-2 0 1 2 3 7 8 11 11

[editar] Ver también

Fusiona dos rangos ordenados.
(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]
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]