Przestrzenie nazw
Warianty

std::forward_list::insert_after

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

Wstawia elementy za określoną pozycję w kontenerze.

1-2) wstawia value za element wskazywany przez pos
3) wstawia count kopii elementu value za element wskazywany przez pos
4) wstawia elementy z zakresu [first, last) za element wskazywany przez pos. Zachowanie jest niezdefiniowane, jeśli first i last są iteratorami po elementach *this.
5) wstawia elementy z listy inicjalizacyjnej ilist za element wskazywany przez pos.

Żadne iteratory ani referencje nie zostają unieważnione.

Parametry

pos - iterator, za którym zostaną wstawione nowe elementy
value - wartość elementu do wstawienia
count - liczba elementów do wstawienia
first, last - zakres, z którego będą wstawiane elementy, nie mogą być to iteratory po kontenerze, który wywołuje tę metodę
ilist - lista inicjalizacyjna, z której będą wstawiane elementy
Wymagania względem typów
-
InputIt musi spełniać wymagania InputIterator.

Zwracana wartość

1-2) Iterator wskazujący na wstawiony element value.
3) Iterator wskazujący na ostatni wstawiony element, lub pos jeśli count==0.
4) Iterator wskazujący na ostatni wstawiony element, lub pos jeśli count==0.
5) Iterator wskazujący na ostatni wstawiony element, lub pos jeśli lista inicjalizacyjna ilist jest pusta.

Wyjątki

Jeśli zostanie wyrzucony wyjątek, zawartość kontenera nie zostanie zmieniona (strong exception guarantee).

Złożoność

1-2) Stała.
3) Liniowa względem count
4) Liniowa względem std::distance(first, last)
5) Liniowa względem ilist.size()

Przykład

#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';                                    
}

Wynik:

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]

Zobacz także

tworzy elementy "w miejscu" za wskazanym elementem
(publiczna metoda) [edit]
wstawia element na początek
(publiczna metoda) [edit]