void splice_after(const_iterator position, forward_list& x); // (1)
void splice_after(const_iterator position, forward_list&& x); // (2)
void splice_after(const_iterator position, forward_list& x,
const_iterator i); // (3)
void splice_after(const_iterator position, forward_list&& x,
const_iterator i); // (4)
void splice_after(const_iterator position, forward_list& x,
const_iterator first, const_iterator last); // (5)
void splice_after(const_iterator position, forward_list&& x,
const_iterator first, const_iterator last); // (6)
概要
他のコンテナから要素を移動する。
要件
- 第1パラメータ
position
が、before_begin()
もしくはイテレータ範囲[begin(), end()
)の間接参照可能なイテレータであること。 i
,first
,last
が、x
のイテレータであること。get_allocator() == x.get_allocator()
であること。(C++14)addressof(x) != this
であること
効果
- (1),(2) :
position
の指す要素の後ろに、x
の全ての要素を移動する - (3),(4) :
position
の指す要素の後ろに、x
の要素のうちi
の次の要素を移動する - (5),(6) :
position
の指す要素の後ろに、x
の要素のうち(first, last)
の範囲を移動する
戻り値
なし
例外
- (1) : 投げない
- (2) : 投げない
- (3) : 投げない
- (4) : 投げない
計算量
- (1), (2) :
x
の要素数に対して線形時間 - (3), (4) : 定数時間
- (5), (6) :
(first, last)
の要素数に対して線形時間
備考
- (1), (2) : この関数を呼び出したあとも、
x
の各要素へのポインタ、参照、イテレータは有効である。ただし、そのポインタと参照は、x
ではなく*this
の要素となる。 - (3), (4) : この関数を呼び出したあとも、
*++i
へのポインタ、参照、イテレータは有効である。ただし、そのポインタと参照は、x
ではなく*this
の要素となる。 - (5), (6) : この関数を呼び出したあとも、
(first, last)
の各要素へのポインタ、参照、イテレータは有効である。ただし、そのポインタと参照は、x
ではなく*this
の要素となる。
例
#include <iostream>
#include <forward_list>
#include <utility>
#include <iterator>
template <class T>
void print(const std::forward_list<T>& ls)
{
for (const T& x : ls) { std::cout << x << ' '; }
std::cout << std::endl;
}
int main()
{
// ysの全ての要素をxsに移動する
{
std::forward_list<int> xs = {1, 5, 6};
std::forward_list<int> ys = {2, 3, 4};
xs.splice_after(xs.begin(), std::move(ys));
print(xs);
}
// ysのうち、3だけを移動する
{
std::forward_list<int> xs = {1, 5, 6};
std::forward_list<int> ys = {2, 3, 4};
xs.splice_after(xs.begin(), std::move(ys), ys.begin());
print(xs);
}
// ysのうち、2と3だけを移動する
{
std::forward_list<int> xs = {1, 5, 6};
std::forward_list<int> ys = {2, 3, 4};
xs.splice_after(xs.begin(), std::move(ys), ys.before_begin(), std::next(ys.begin(), 2));
print(xs);
}
}
出力
1 2 3 4 5 6
1 3 5 6
1 2 3 5 6
バージョン
言語
- C++11
処理系
- Clang: ??
- GCC: 4.7.0(右辺値参照バージョンのみ実装されている)
- ICC: ??
- Visual C++: ??
参照
- LWG Issue 2045.
forward_list::merge
andforward_list::splice_after
with unequal allocators - LWG Issue 2222. Inconsistency in description of
forward_list::splice_after
single-element overload- (3)と(4)のオーバーロードについて、有効性が継続される対象が、
*i
となっていたが、*++i
の間違いであったため、C++14で修正された。
- (3)と(4)のオーバーロードについて、有効性が継続される対象が、
- LWG Issue 3017.
list splice
functions should useaddressof