void splice(iterator position, list& x); // (1) C++03
void splice(const_iterator position, list& x); // (1) C++11
void splice(const_iterator position, list&& x); // (2) C++11
void splice(iterator position, list& x,
iterator i); // (3) C++03
void splice(const_iterator position, list& x,
const_iterator i); // (3) C++11
void splice(const_iterator position, list&& x,
const_iterator i); // (4) C++11
void splice(iterator position, list& x,
iterator first, iterator last); // (5) C++03
void splice(const_iterator position, list& x,
const_iterator first, const_iterator last); // (5) C++11
void splice(const_iterator position, list&& x,
const_iterator first, const_iterator last); // (6) C++11
概要
他のlistオブジェクトから、要素を移動する。
- (1), (2) :
positionの指す要素の前に、xの全ての要素を移動する - (3), (4) :
positionの指す要素の前に、xの要素のうちiの指す要素を移動する - (5), (6) :
positionの指す要素の前に、xの要素のうち[first, last)の範囲を移動する
要件
- 第1パラメータ
positionが、イテレータ範囲[begin(), end())の間接参照可能なイテレータであること。 i,first,lastが、xのイテレータであること。- (1), (2) :
- C++03 :
&x != thisであること - C++11 :
addressof(x) != thisであること
- C++03 :
- (5), (6) :
positionが[first, last)に含まれる場合、未定義動作。
戻り値
なし
計算量
- C++03まで
- (1) :
xの要素数に対して線形時間 - (3) : 定数時間
- (5) :
[first, last)の要素数に対して線形時間
- (1) :
- C++11から
- (1), (2) : 定数時間
- (3), (4) : 定数時間
- (5), (6) :
addressof(x) == thisの場合、定数時間。そうでない場合、[first, last)の要素数に対して線形時間
例外
- 投げない
備考
- (1), (2) : C++03の場合、移動元の
listオブジェクトxは、全てのイテレータと参照が無効になる。C++11以降は、無効にならない。 - (3), (4) : C++03の場合、移動元の要素を指すイテレータ
iは、そのイテレータおよび参照が無効になる。C++11以降は、無効にならない。 - (5), (6) : C++03の場合、移動元のイテレータ範囲
[first, last)は、その範囲のイテレータおよび参照が無効になる。C++11以降は、無効にならない。 - 移動先と移動元のアロケータが等値でない場合(
get_allocator() != x.get_allocator()の場合)、動作は未定義である。
例
#include <iostream>
#include <list>
template <class T>
void print(const std::list<T>& ls)
{
for (const T& x : ls) { std::cout << x << ' '; }
std::cout << std::endl;
}
int main()
{
// ysの全ての要素をxsに移動する
{
std::list<int> xs = {4, 5, 6};
std::list<int> ys = {1, 2, 3};
xs.splice(xs.begin(), ys);
print(xs);
}
// ysのうち、1だけを移動する
{
std::list<int> xs = {4, 5, 6};
std::list<int> ys = {1, 2, 3};
xs.splice(xs.begin(), ys, ys.begin());
print(xs);
}
// ysのうち、1と2だけを移動する
{
std::list<int> xs = {4, 5, 6};
std::list<int> ys = {1, 2, 3};
xs.splice(xs.begin(), ys, ys.begin(), std::next(ys.begin(), 2));
print(xs);
}
// ysの全ての要素をxsの末尾に移動する
{
std::list<int> xs = {1, 2, 3};
std::list<int> ys = {4, 5, 6};
xs.splice(xs.end(), ys);
print(xs);
}
}
出力
1 2 3 4 5 6
1 4 5 6
1 2 4 5 6
1 2 3 4 5 6