std::forward_list<T,Allocator>:: insert_after
|
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,
|
(4) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
|
iterator insert_after
(
const_iterator pos,
std:: initializer_list < T > ilist ) ; |
(5) |
(自 C++11 起)
(自 C++26 起为 constexpr) |
在容器的指定位置之后插入元素。如果 pos 是 before_begin() ,则插入的第一个元素(如果存在)将成为 * this 的第一个元素。
如果
pos
不在范围
[
before_begin()
,
end()
)
内,则行为未定义。
[
first
,
last
)
中的元素。
-
T无法从 * first 进行 EmplaceConstructible 到forward_list中。 - first 或 last 是指向 * this 的迭代器。
不会使任何迭代器或引用失效。
目录 |
参数
| pos | - | 插入位置后的迭代器 |
| value | - | 要插入的元素值 |
| count | - | 要插入的副本数量 |
| first, last | - | 定义要插入元素来源 范围 的迭代器对 |
| ilist | - | 要插入值的初始化列表 |
返回值
异常
若因任何原因抛出异常,这些函数将不产生任何效果( 强异常安全保证 )。
复杂度
示例
#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}
参见
|
在指定元素后原位构造元素
(公开成员函数) |
|
|
在容器开头插入元素
(公开成员函数) |