std::list<T,Allocator>::splice
From cppreference.com
| void splice( const_iterator pos, list& other ); |
(1) | |
| void splice( const_iterator pos, list&& other ); |
(2) | (C++11以降) |
| void splice( const_iterator pos, list& other, const_iterator it ); |
(3) | |
| void splice( const_iterator pos, list&& other, const_iterator it ); |
(4) | (C++11以降) |
| void splice( const_iterator pos, list& other, const_iterator first, const_iterator last); |
(5) | |
| void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last ); |
(6) | (C++11以降) |
リスト間で要素を移動させます。
要素のコピーや移動は行われず、リストノードの内部ポインタのみが再設定されます。イテレータや参照が無効になることはありません。移動された要素を指すイテレータは有効なままで、もはやotherではなく*thisを参照します。
1,2)
otherの全要素を*thisに移動させます。要素はposが指す要素の前に挿入されます。操作後、コンテナotherは空になります。3,4)
otherからitが指す要素を*thisに移動させます。要素はposが指す要素の前に挿入されます。5,6)
otherの範囲[first, last)の要素を*thisに移動させます。要素はposが指す要素の前に挿入されます。以下の場合、動作は未定義です:
- O(1) ノード移動を保証できませんでした,
- オーバーロード(1,2)の場合、
*thisとotherは同じオブジェクトを参照します。 - オーバーロード(3,4)の場合、
itはotherに対するdereferenceable iteratorではありません。または、 - オーバーロード(5,6)の場合、
- 範囲
[first, last)はotherの有効な範囲ではありません。または、 -
posが[first, last)の範囲内にあります。
- 範囲
目次 |
[編集] パラメータ
| pos | - | コンテンツが挿入される要素の前 |
| その他 | - | コンテンツを転送する別のコンテナ |
| it | - | otherから*thisへ転送する要素 |
| first, last | - | otherから*thisへ転送する要素の範囲を定義するイテレータのペア |
[編集] 戻り値
(なし)
[編集] 例外
何もスローしません。
[編集] 計算量
1-4)定数時間。
[編集] 例
このコードを実行
#include <iostream> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (auto& i : list) ostr << ' ' << i; return ostr; } int main () { std::list<int> list1{1, 2, 3, 4, 5}; std::list<int> list2{10, 20, 30, 40, 50}; auto it = list1.begin(); std::advance(it, 2); list1.splice(it, list2); std::cout << "list1:" << list1 << '\n'; std::cout << "list2:" << list2 << '\n'; list2.splice(list2.begin(), list1, it, list1.end()); std::cout << "list1:" << list1 << '\n'; std::cout << "list2:" << list2 << '\n'; }
出力
list1: 1 2 10 20 30 40 50 3 4 5 list2: list1: 1 2 10 20 30 40 50 list2: 3 4 5
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 250 | C++98 | 移動された要素の参照とイテレータはすべて無効になりました。 それらは *thisの同じ要素を参照または指します。 |
それらは*thisの同じ要素を参照または指します。それらは *thisの同じ要素を参照または指します。 |
| N2525 | C++98 | otherが*thisと同じオブジェクトを参照する場合、O(1)のスプライシングは保証できませんでした。O(1) ノード移動を保証できませんでした |
この場合の動作は 未定義となる |
[編集] 関連項目
| ソート済みの2つのリストをマージする (公開メンバ関数) | |
| 特定の基準を満たす要素を削除する (public member function) |