名前空間
変種
操作

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

提供: cppreference.com
 
 
 
 
iterator insert_after( const_iterator pos, const T& value );
(1) (C++11以上)
iterator insert_after( const_iterator pos, T&& value );
(2) (C++11以上)
iterator insert_after( const_iterator pos, size_type count, const T& value );
(3) (C++11以上)
template< class InputIt >
iterator insert_after( const_iterator pos, InputIt first, InputIt last );
(4) (C++11以上)
iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );
(5) (C++11以上)

コンテナ内の指定された位置の後に要素を挿入します。

1-2) pos の指す要素の後に value を挿入します。
3) pos の指す要素の後に value のコピーを count 個挿入します。
4) pos の指す要素の後に範囲 [first, last) から要素を挿入します。 first および last*this 内を指すイテレータの場合、動作は未定義です。
5) 初期化子リスト ilist から要素を挿入します。

どのイテレータも参照も無効化されません。

目次

[編集] 引数

pos - 後に内容が挿入されるイテレータ
value - 挿入する要素の値
count - 挿入するコピーの数
first, last - 挿入する要素の範囲
ilist - 挿入する値の初期化子リスト
型の要件
-
InputItLegacyInputIterator の要件を満た��なければなりません。

[編集] 戻り値

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

[編集] 例外

insert_after の間に例外が投げられた場合、効果はありません (強い例外保証)。

[編集] 計算量

1-2) 一定。
3) count に比例。
4) std::distance(first, last) に比例。
5) ilist.size() に比例。

[編集]

#include <forward_list>                                                         
#include <string>                                                               
#include <iostream>                                                             
#include <vector>                                                               
 
template<typename T>                                                            
std::ostream& operator<<(std::ostream& s, const std::forward_list<T>& v) {      
    s.put('[');                                                                 
    char comma[3] = {'\0', ' ', '\0'};                                          
    for (const auto& e : v) {                                                   
        s << comma << e;                                                        
        comma[0] = ',';                                                         
    }                                                                           
    return s << ']';                                                            
}                                                                               
 
int main()                                                                      
{                                                                               
    std::forward_list<std::string> words {"the", "frogurt", "is", "also", "cursed"};
    std::cout << "words: " << words << '\n';                                    
 
    // insert_after (2)                                                         
    auto beginIt = words.begin();                                               
    words.insert_after(beginIt, "strawberry");                                  
    std::cout << "words: " << words << '\n';                                    
 
    // insert_after (3)                                                         
    auto anotherIt = beginIt;                                                   
    ++anotherIt;                                                                
    anotherIt = words.insert_after(anotherIt, 2, "strawberry");                 
    std::cout << "words: " << words << '\n';                                    
 
    // insert_after (4)
    std::vector<std::string> V = { "apple", "banana", "cherry"};                
    anotherIt = words.insert_after(anotherIt, V.begin(), V.end());              
    std::cout << "words: " << words << '\n';                                    
 
    // insert_after (5)                                                         
    words.insert_after(anotherIt, {"jackfruit", "kiwifruit", "lime", "mango"});
    std::cout << "words: " << words << '\n';                                    
}

出力:

words: [the, frogurt, is, also, cursed]
words: [the, strawberry, frogurt, is, also, cursed]
words: [the, strawberry, strawberry, strawberry, frogurt, is, also, cursed]
words: [the, strawberry, strawberry, strawberry, apple, banana, cherry, frogurt, is, also, cursed]
words: [the, strawberry, strawberry, strawberry, apple, banana, cherry, jackfruit, kiwifruit, lime, mango, frogurt, is, also, cursed]

[編集] 関連項目

要素の後に要素を直接構築します
(パブリックメンバ関数) [edit]
要素を先頭に挿入します
(パブリックメンバ関数) [edit]