首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::forward_list::insert_after

iterator insert_after( const_iterator pos, const T& value );

(1)

(since C++11)

iterator insert_after( const_iterator pos, T&& value );

(2)

(since C++11)

iterator insert_after( const_iterator pos, size_type count, const T& value );

(3)

(since C++11)

template< class InputIt > iterator insert_after( const_iterator pos, InputIt first, InputIt last );

(4)

(since C++11)

iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );

(5)

(since C++11)

在容器中指定位置之后插入元素。

1-2%29次插入value在指向的元素之后pos

3%29次插入count的副本value在指向的元素之后pos

4%29插入范围内的元素[first, last)在指向的元素之后pos如果firstlast迭代器是否进入*this...

5%29从初始化程序列表插入元素ilist...

没有迭代器或引用无效。

参数

pos

-

iterator after which the content will be inserted

value

-

element value to insert

count

-

number of copies to insert

first, last

-

the range of elements to insert

ilist

-

initializer list to insert the values from

类型要求

-输入必须符合输入器的要求。

返回值

插入元素1-2%29迭代器.

3%29 Iterator到最后插入的元素,或pos如果count==0...

4%29 Iterator到最后插入的元素,或pos如果first==last...

5%29 Iterator到最后插入的元素,或pos如果ilist是空的。

例外

如果在insert_after没有效果%28强异常保证%29。

复杂性

1-2%29常数。

3%29线性count

4%29线性std::distance(first, last)

5%29线性ilist.size()

二次

代码语言:javascript
复制
#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';                                    
}

二次

产出:

二次

代码语言:javascript
复制
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]

二次

另见

emplace_after

constructs elements in-place after an element (public member function)

push_front

inserts an element to the beginning (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券