std::forward_list<T,Allocator>::insert_after
來自 cppreference.com
< 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) |