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 <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(), 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(), 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(), 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::mergeandforward_list::splice_afterwith unequal allocators - LWG Issue 2222. Inconsistency in description of
forward_list::splice_aftersingle-element overload- (3)と(4)のオーバーロードについて、有効性が継続される対象が、
*iとなっていたが、*++iの間違いであったため、C++14で修正された。
- (3)と(4)のオーバーロードについて、有効性が継続される対象が、
- LWG Issue 3017.
list splicefunctions should useaddressof