std::inplace_merge
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in 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) | |
[first, middle)
と[middle, last)
1ソート範囲[first, last)
に2つの連続した並べ替えの範囲をマージします。等しい要素の順序は保持されることが保証されている。最初のバージョンは、要素を比較するoperator<使用して、2番目のバージョンは、指定された比較関数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.
目次 |
[編集]パラメータ
first | - | 最初にソート範囲の先頭 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 | - | 最初にソート範囲と第2の始まりの終わり 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 | - | 第二ソート範囲の終わり 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 | - | 比較関数. 最初の値が二つ目の値より小さい 場合、 trueを返します. 比較関数のシグネチャは以下と同等でなければなりません. bool cmp(const Type1 &a, const Type2 &b); シグネチャは |
型の要件 | ||
-BidirIt は ValueSwappable and BidirectionalIterator 。 | ||
-The type of dereferenced BidirIt must meet the requirements of MoveAssignable and MoveConstructible . |
[編集]値を返します
(なし)
[編集]複雑性
まさにN-1比較に十分な追加メモリが使用可能であれば、そうでなければN·log(N)どこ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.
[編集]ノート
この関数は、一般的にstd::get_temporary_bufferを呼び出すことによって、一時的なバッファを割り当てようとします。割り当てが失敗した場合は、あまり効率的なアルゴリズムが選ばれ.
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.
[編集]例
次のコードはマージソートを実装したものです.
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';}
出力:
-2 0 1 2 3 7 8 11 11
[編集]参照
2つのソートされた範囲をマージします 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. (関数テンプレート) | |
昇順にソートする範囲 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. (関数テンプレート) | |
ソートされた範囲の要素を等しい要素間の順序を維持しながら 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. (関数テンプレート) |