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

来自cppreference.com
 
 
 
 
iterator insert_after( const_iterator pos, const T& value );
(1) (C++11 起)
(C++26 起为 constexpr)
iterator insert_after( const_iterator pos, T&& value );
(2) (C++11 起)
(C++26 起为 constexpr)
iterator insert_after( const_iterator pos,
                       size_type count, const T& value );
(3) (C++11 起)
(C++26 起为 constexpr)
template< class InputIt >

iterator insert_after( const_iterator pos,

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

在容器中的指定位置后插入元素。如果 posbefore_begin(),那么插入的首个元素(如果存在)就会成为 *this 的首个元素。

如果 pos 不在范围 [before_begin()end()) 中,那么行为未定义。

1,2)pos 后插入 value 的副本。
1) 如果 T可复制插入 (CopyInsertable) forward_list 中,那么行为未定义。
2) 如果 T可移动插入 (MoveInsertable) forward_list 中,那么行为未定义。
3)pos 所指向的元素后插入 valuecount 个副本。
如果 T可复制插入 (CopyInsertable) forward_list 中,那么行为未定义。
4)pos 所指向的元素后插入来自范围 [firstlast) 的元素。
此重载只有在 InputIt 满足老式输入迭代器 (LegacyInputIterator) 的要求时才会参与重载决议。
如果满足以下任意条件,那么行为未定义:
5) 插入来自初始化器列表 ilist 的元素。
等价于 return insert_after(position, ilist.begin(), ilist.end());

没有引用和迭代器会失效。

目录

[编辑] 参数

pos - 内容将插入到其后的迭代器
value - 要插入的元素值
count - 要插入的副本数
first, last - 要插入的源元素范围的迭代器对
ilist - 插入值来源的初始化式列表
类型要求

[编辑] 返回值

1,2) 指向被插入元素的迭代器。
3) 指向最后被插入元素的迭代器,或在 count == 0true 时返回 pos
4) 指向最后被插入元素的迭代器,或在 first == lasttrue 时返回 pos
5) 指向最后被插入元素的迭代器,或在 ilist 为空时返回 pos

[编辑] 异常

如果因为任何原因抛出了异常,那么这些函数无效果(强异常安全保证)。

[编辑] 复杂度

1,2) 常数。
3)count 成线性。
4)std::distance(first, last) 成线性。
5)ilist.size() 成线性。

[编辑] 示例

#include <forward_list>                                                         
#include <string>                                                               
#include <iostream>                                                             
#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}

[编辑] 参阅

在元素后原位构造元素
(公开成员函数) [编辑]
插入元素到容器起始
(公开成员函数) [编辑]