std::inplace_merge — cppreference.com
De cppreference.com
<metanoindex/>
<tbody> </tbody>
| Déclaré dans l'en-tête <algorithm> |
||
|
|
(1) | |
|
|
(2) | |
Fusionne deux mandats consécutifs de gammes triés [first, middle) et [middle, last) dans une gamme [first, last) triés. L'ordre des éléments égaux est assuré d'être préservé. La première version utilise operator< de comparer les éléments, la deuxième version utilise la fonction de comparaison donnée 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.
Paramètres
| first | - | le début de la première plage triés Original: the beginning of the first sorted range The text has been machine-translated via Google Translate. |
| middle | - | la fin de la première plage triés et le début de la seconde Original: the end of the first sorted range and the beginning of the second The text has been machine-translated via Google Translate. |
| last | - | la fin de la seconde plage triés Original: the end of the second sorted range The text has been machine-translated via Google Translate. |
| 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:
The signature does not need to have |
| Type requirements | ||
-BidirIt must meet the requirements of ValueSwappable and BidirectionalIterator.
| ||
-The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible.
| ||
Retourne la valeur
(Aucun)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Complexité
Exactement N-1 comparaisons si suffisamment de mémoire supplémentaire est disponible, sinon N·log(N) où 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.
Notes
Cette fonction tente d'allouer une mémoire tampon temporaire, généralement en appelant std::get_temporary_buffer. Si l'allocation échoue, l'algorithme moins efficace est choisi .
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.
Exemple
Le code suivant est une implémentation du tri par fusion .
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'; }
Résultat :
Voir aussi
fusionne deux gammes triés Original: merges two sorted ranges The text has been machine-translated via Google Translate. (fonction générique) [edit] | |
trie une plage dans l'ordre ascendant Original: sorts a range into ascending order The text has been machine-translated via Google Translate. (fonction générique) [edit] | |
Trie une plage d'éléments, tout en préservant l'ordre entre des éléments égaux Original: sorts a range of elements while preserving order between equal elements The text has been machine-translated via Google Translate. (fonction générique) [edit] | |