std::inplace_merge
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <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) | |
Unisce due intervalli consecutivi ordinati
[first, middle)
e [middle, last)
in una gamma [first, last)
ordinati. L'ordine degli elementi uguali è garantito per essere preservato. La prima versione utilizza operator< di confrontare gli elementi, la seconda versione utilizza la funzione di confronto dato 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.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Parametri
first | - | l'inizio del primo intervallo ordinato
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 | - | alla fine del primo intervallo ordinati e l'inizio del secondo
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 | - | la fine del secondo intervallo ordinato
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 | - | comparison function which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
Type requirements | ||
-BidirIt must meet the requirements of ValueSwappable and BidirectionalIterator .
| ||
-The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible .
|
[modifica] Valore di ritorno
(Nessuno)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Complessità
Esattamente N-1 confronti se un numero sufficiente di memoria aggiuntiva è disponibile, altrimenti N·log(N) dove 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.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Note
Questa funzione tenta di allocare un buffer temporaneo, di solito chiamando std::get_temporary_buffer. Se l'allocazione non riesce, l'algoritmo meno efficiente è scelto.
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.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Esempio
Il seguente codice è un'implementazione di merge sort .
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.
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'; }
Output:
-2 0 1 2 3 7 8 11 11
[modifica] Vedi anche
unisce due campi ordinati Original: merges two sorted ranges The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
ordina una serie in ordine crescente 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. (funzione di modello) | |
Ordina un intervallo di elementi, mantenendo ordine tra elementi uguali 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. (funzione di modello) |