Namespaces
Variants

std::forward_list<T,Allocator>:: insert_after

From cppreference.net
iterator insert_after ( const_iterator pos, const T & value ) ;
(1) (C++11以降)
(constexpr since C++26)
iterator insert_after ( const_iterator pos, T && value ) ;
(2) (C++11以降)
(constexpr since C++26)
iterator insert_after ( const_iterator pos,
size_type count, const T & value ) ;
(3) (C++11以降)
(constexpr since C++26)
template < class InputIt >

iterator insert_after ( const_iterator pos,

InputIt first, InputIt last ) ;
(4) (C++11以降)
(constexpr since C++26)
iterator insert_after ( const_iterator pos,
std:: initializer_list < T > ilist ) ;
(5) (C++11以降)
(constexpr since C++26)

指定された位置の後に要素をコンテナに挿入します。 pos before_begin() の場合、最初に挿入された要素(存在すれば)は * this の最初の要素になります。

pos が範囲 [ before_begin() , end() ) 内にない場合、動作は未定義です。

1,2) pos の後に value のコピーを挿入します。
1) T CopyInsertable でない場合、動作は未定義です。
2) T MoveInsertable でない場合、動作は未定義です。
3) pos の後に value count 個のコピーを挿入します。
T CopyInsertable でない場合、 forward_list への挿入時の動作は未定義です。
4) 範囲 [ first , last ) の要素を pos の後に挿入します。
このオーバーロードは、 InputIt LegacyInputIterator の要件を満たす場合にのみ、オーバーロード解決に参加します。
以下のいずれかの条件が満たされる場合、動作は未定義です:
  • T EmplaceConstructible から forward_list * first によって構築可能でない場合。
  • first または last * this へのイテレータである場合。
5) 初期化子リストから要素を挿入 ilist の後ろに pos を配置します。
次と同等 return insert_after ( position, ilist. begin ( ) , ilist. end ( ) ) ;

イテレータや参照は無効化されません。

目次

パラメータ

pos - コンテンツを挿入する位置の後ろを指すイテレータ
value - 挿入する要素の値
count - 挿入するコピーの数
first, last - 挿入する要素のソース 範囲 を定義するイテレータのペア
ilist - 値を挿入する初期化子リスト

戻り値

1,2) 挿入された要素へのイテレータ。
3) 挿入された最後の要素へのイテレータ、または pos count == 0 の場合。
4) 挿入された最後の要素へのイテレータ、または pos first == last の場合 true です。
5) 挿入された最後の要素へのイテレータ、または pos ilist 空の場合。

例外

何らかの理由で例外がスローされた場合、これらの関数は何も効果を持ちません( strong exception safety guarantee )。

計算量

1,2) 定数。
3) 線形( count に比例)。
4) std:: distance ( first, last ) の計算量は線形です。
5) 線形で ilist. size ( ) に比例します。

#include <forward_list>
#include <iostream>
#include <string>
#include <vector>
void print(const std::forward_list<int>& list)
{
    std::cout << "list: {";
    for (char comma[3] = {'\0', ' ', '\0'}; int i : list)
    {
        std::cout << comma << i;
        comma[0] = ',';
    }
    std::cout << "}\n";
}
int main()
{
    std::forward_list<int> ints{1, 2, 3, 4, 5};
    print(ints);
    // insert_after (2)
    auto beginIt = ints.begin();
    ints.insert_after(beginIt, -6);
    print(ints);
    // insert_after (3)
    auto anotherIt = beginIt;
    ++anotherIt;
    anotherIt = ints.insert_after(anotherIt, 2, -7);
    print(ints);
    // insert_after (4)
    const std::vector<int> v = {-8, -9, -10};
    anotherIt = ints.insert_after(anotherIt, v.cbegin(), v.cend());
    print(ints);
    // insert_after (5)
    ints.insert_after(anotherIt, {-11, -12, -13, -14});
    print(ints);
}

出力:

list: {1, 2, 3, 4, 5}
list: {1, -6, 2, 3, 4, 5}
list: {1, -6, -7, -7, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, 2, 3, 4, 5}
list: {1, -6, -7, -7, -8, -9, -10, -11, -12, -13, -14, 2, 3, 4, 5}

関連項目

要素を要素の直後にその場で構築する
(公開メンバ関数)
要素を先頭に挿入する
(公開メンバ関数)