std::list<T,Allocator>::splice
提供: cppreference.com
void splice( const_iterator pos, list& other ); |
(1) | |
void splice( const_iterator pos, list&& other ); |
(1) | (C++11以上) |
void splice( const_iterator pos, list& other, const_iterator it ); |
(2) | |
void splice( const_iterator pos, list&& other, const_iterator it ); |
(2) | (C++11以上) |
void splice( const_iterator pos, list& other, const_iterator first, const_iterator last); |
(3) | |
void splice( const_iterator pos, list&& other, const_iterator first, const_iterator last ); |
(3) | (C++11以上) |
要素をリストから別のリストに転送します。
要素はコピーもムーブもされません。 リストのノードの内部ポインタが指し変えられるだけです。 get_allocator() != other.get_allocator() の場合、動作は未定義です。 どのイテレータも参照も無効化されません。 移動される要素を指すイテレータは有効なままですが、 other ではなく *this 内を指すようになります。
1) すべての要素を
other から *this に転送します。 要素は pos の指す要素の前に挿入されます。 操作後、コンテナ other は空になります。 other が *this と同じオブジェクトを参照する場合、動作は未定義です。2)
it の指す要素を other から *this に転送します。 要素は pos の指す要素の前に挿入されます。3) 範囲
[first, last) の要素を other から *this に転送します。 要素は pos の指す要素の前に挿入されます。 pos が範囲 [first,last) 内のイテレータである場合、動作は未定義です。引数
| pos | - | 前に内容が挿入される要素 |
| other | - | 内容を転送する別のコンテナ |
| it | - | other から *this に転送する要素
|
| first, last | - | other から *this に転送する要素の範囲
|
戻り値
(なし)
例外
何も投げません。
計算量
1-2) 一定。
3) other が *this と同じオブジェクトを参照する場合は一定。 そうでなければ std::distance(first, last) に比例。
例
Run this code
#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
関連項目
| ソートされた2つのリストをマージします (パブリックメンバ関数) | |
| 特定の基準を満たす要素を削除します (パブリックメンバ関数) |