std::forward_list<T,Allocator>::insert_after
来自 cppreference.cn
< cpp | 容器 | forward_list
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 指向的元素之后插入 count 份 value 的副本。
4) 在 pos 指向的元素之后插入来自范围
[
first,
last)
的元素。如果 first 和 last 是指向 *this 的迭代器,则行为未定义。5) 插入来自初始化列表 ilist 的元素。
迭代器或引用均未失效。
目录 |
[edit] 参数
pos | - | 内容将被插入的迭代器位置 |
value | - | 要插入的元素值 |
count | - | 要插入的副本数量 |
first, last | - | 定义要插入的元素源范围的迭代器对 |
ilist | - | 要从中插入值的初始化列表 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 |
[edit] 返回值
1,2) 指向被插入元素的迭代器。
3) 指向最后一个被插入元素的迭代器,如果 count == 0 则为 pos。
4) 指向最后一个被插入元素的迭代器,如果 first == last 则为 pos。
5) 指向最后一个被插入元素的迭代器,如果 ilist 为空则为 pos。
[edit] 异常
如果因任何原因抛出异常,这些函数没有效果(强异常安全保证)。
[edit] 复杂度
1,2) 常数时间。
3) 与 count 成线性关系。
4) 与 std::distance(first, last) 成线性关系。
5) 与 ilist.size() 成线性关系。
[edit] 示例
运行此代码
#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}
[edit] 参阅
在元素之后就地构造元素 (public member function) | |
插入元素到起始 (public member function) |