std::deque<T,Allocator>::insert - cppreference.com (original) (raw)
| iterator insert( const_iterator pos, const T& value ); | (1) | (constexpr since C++26) |
|---|---|---|
| iterator insert( const_iterator pos, T&& value ); | (2) | (since C++11) (constexpr since C++26) |
| iterator insert( const_iterator pos, size_type count, const T& value ); | (3) | (constexpr since C++26) |
| template< class InputIt >iterator insert( const_iterator pos, InputIt first, InputIt last ); | (4) | (constexpr since C++26) |
| iterator insert( const_iterator pos, std::initializer_list<T> ilist ); | (5) | (since C++11) (constexpr since C++26) |
Inserts elements at the specified location in the container.
- Inserts a copy of value before pos.
If any of the following conditions is satisfied, the behavior is undefined:
Tis not CopyAssignable.
Inserts value before pos, possibly using move semantics.
Inserts count copies of the value before pos.
If any of the following conditions is satisfied, the behavior is undefined:
Tis not CopyAssignable.
- Inserts elements from range
[first,last)before pos.
| This overload has the same effect as overload (3) if InputIt is an integral type. | (until C++11) |
|---|---|
| This overload participates in overload resolution only if InputIt satisfies the requirements of LegacyInputIterator. | (since C++11) |
- Inserts elements from initializer list ilist before pos.
Equivalent to insert(pos, ilist.begin(), ilist.end()).
All iterators (including the end() iterator) are invalidated. References are invalidated too, unless pos == begin() or pos == end(), in which case they are not invalidated.
Contents
[edit] Parameters
| pos | - | iterator before which the content will be inserted |
|---|---|---|
| value | - | element value to insert |
| count | - | number of elements to insert |
| first, last | - | the pair of iterators defining the range of elements to insert |
| ilist | - | std::initializer_list to insert the values from |
[edit] Return value
1,2) Iterator pointing to the inserted value.
3-5) Iterator pointing to the first element inserted, or pos if no element is inserted.
[edit] Complexity
Linear in the number of elements inserted plus std::min(std::distance(begin(), pos), std::distance(pos, end())).
[edit] Exceptions
If an exception is thrown other than by
- the copy constructor of
T,
| the move constructor of T, | (since C++11) |
|---|
- the copy assignment operator of
T,
| the move assignment operator of T, | (since C++11) |
|---|
this function has no effect (strong exception guarantee).
| If an exception is thrown when inserting a single element at either end, this function has no effect (strong exception guarantee). Otherwise, if an exception is thrown by the move constructor of a non-CopyInsertable T, the effects are unspecified. | (since C++11) |
|---|
[edit] Example
#include #include #include #include namespace stq { void println(std::string_view rem, const std::deque& container) { std::cout << rem.substr(0, rem.size() - 2) << '['; bool first{true}; for (const int x : container) std::cout << (first ? first = false, "" : ", ") << x; std::cout << "]\n"; } } int main() { std::deque c1(3, 100); stq::println("1. {}", c1); auto pos = c1.begin(); pos = c1.insert(pos, 200); // overload (1) stq::println("2. {}", c1); c1.insert(pos, 2, 300); // overload (3) stq::println("3. {}", c1); // reset pos to the begin: pos = c1.begin(); std::deque c2(2, 400); c1.insert(std::next(pos, 2), c2.begin(), c2.end()); // overload (4) stq::println("4. {}", c1); int arr[] = {501, 502, 503}; c1.insert(c1.begin(), arr, arr + std::size(arr)); // overload (4) stq::println("5. {}", c1); c1.insert(c1.end(), {601, 602, 603}); // overload (5) stq::println("6. {}", c1); }
Output:
- [100, 100, 100]
- [200, 100, 100, 100]
- [300, 300, 200, 100, 100, 100]
- [300, 300, 400, 400, 200, 100, 100, 100]
- [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100]
- [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100, 601, 602, 603]
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 149 | C++98 | overloads (3) and (4) returned nothing | returns an iterator |
| LWG 247 | C++98 | the complexity was only specifiedfor inserting a single element | also specified for insertingmultiple elements |