std::forward_list<T,Allocator>:: merge
|
void
merge
(
forward_list
&
other
)
;
|
(1) |
(C++11以降)
(constexpr C++26以降) |
|
void
merge
(
forward_list
&&
other
)
;
|
(2) |
(C++11以降)
(constexpr C++26以降) |
|
template
<
class
Compare
>
void merge ( forward_list & other, Compare comp ) ; |
(3) |
(C++11以降)
(constexpr C++26以降) |
|
template
<
class
Compare
>
void merge ( forward_list && other, Compare comp ) ; |
(4) |
(C++11以降)
(constexpr C++26以降) |
2つのソート済みリストを1つのソート済みリストにマージします。
- other が * this と同じオブジェクトを参照している場合、何も行いません。
- それ以外の場合、 other から * this へ全ての要素を転送します。 other はマージ後に空になります。
この操作は安定しています:
- 二つのリストの等価な要素について、 * this の要素は常に other の要素より前に配置されます。
- * this と other の等価な要素の順序は変更されません。
- * this または other がコンパレータ comp に関して ソートされていない 場合。
- get_allocator ( ) == other. get_allocator ( ) が false の場合。
イテレータや参照は無効化されません。 * this から移動された要素へのポインタと参照、およびこれらの要素を参照するイテレータは、 other ではなく * this の同じ要素を参照します。
目次 |
パラメータ
| other | - | マージする別のコンテナ |
| comp | - |
比較関数オブジェクト(つまり
Compare
の要件を満たすオブジェクト)。最初の引数が2番目の引数より
小さい
(つまり
前に順序付けられる
)場合に
true
を返す。
比較関数のシグネチャは以下と同等であるべきです: bool cmp ( const Type1 & a, const Type2 & b ) ;
シグネチャが
const
&
を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、
値カテゴリ
に関係なく型(おそらくconstの)
|
| 型の要件 | ||
-
Compare
は
Compare
の要件を満たさなければならない。
|
||
例外
何らかの理由で例外がスローされた場合、これらの関数は何も効果を持ちません( strong exception safety guarantee )。ただし、例外が比較操作から発生した場合は除きます。
計算量
other が * this と同じオブジェクトを参照している場合、比較は実行されません。
それ以外の場合、 N 1 を std:: distance ( begin ( ) , end ( ) ) とし、 N 2 を std:: distance ( other. begin ( ) , other. end ( ) ) とする:
例
#include <iostream> #include <forward_list> std::ostream& operator<<(std::ostream& ostr, const std::forward_list<int>& list) { for (const int i : list) ostr << ' ' << i; return ostr; } int main() { std::forward_list<int> list1 = {5, 9, 1, 3, 3}; std::forward_list<int> list2 = {8, 7, 2, 3, 4, 4}; list1.sort(); list2.sort(); std::cout << "list1: " << list1 << '\n'; std::cout << "list2: " << list2 << '\n'; list1.merge(list2); std::cout << "merged:" << list1 << '\n'; }
出力:
list1: 1 3 3 5 9 list2: 2 3 4 4 7 8 merged: 1 2 3 3 3 4 4 5 7 8 9
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2045 | C++11 |
O(1) ノード移動が保証されなかった場合
get_allocator ( ) ! = other. get_allocator ( ) |
この場合の動作は
未定義 |
| LWG 3088 | C++11 |
*
this
と
other
が同じオブジェクトを
参照する場合の効果が規定されていなかった operator < がポインタ要素で誤動作する可能性があった |
ノーオペレーションとして規定
実装定義 厳密な全順序が使用される |
関連項目
別の
forward_list
から要素を転送する
(公開メンバ関数) |
|
|
二つのソート済み範囲をマージする
(関数テンプレート) |
|
|
二つの順序付けられた範囲をその場でマージする
(関数テンプレート) |
|
|
(C++20)
|
二つのソート済み範囲をマージする
(アルゴリズム関数オブジェクト) |
|
(C++20)
|
二つの順序付けられた範囲をその場でマージする
(アルゴリズム関数オブジェクト) |