std::vector<T,Allocator>::insert
(constexpr since C++20)
size_type count, const T& value );
iterator insert( const_iterator pos, InputIt first, InputIt last );
(constexpr since C++20)
Inserts elements at the specified location in the container.
-
T
is not CopyInsertable intovector
.
-
T
is not CopyAssignable.
-
T
is not MoveInsertable intovector
. -
T
is not MoveAssignable.
-
T
is not CopyInsertable intovector
.
-
T
is not CopyAssignable.
[
first,
last)
before pos.
This overload has the same effect as overload (3) if InputIt
is an integral type.
This overload participates in overload resolution only if InputIt
satisfies the requirements of LegacyInputIterator.
-
T
is not EmplaceConstructible intovector
from *first. -
T
is not MoveInsertable intovector
. -
T
does not satisfy the requirements of MoveConstructible, MoveAssignable or Swappable.
- first or last are iterators into *this.
If after the operation the new size()
is greater than old capacity()
a reallocation takes place, in which case all iterators (including the end()
iterator) and all references to the elements are invalidated. Otherwise, only the iterators and references before the insertion point remain valid.
Contents
[edit] Parameters
[edit] Return value
[edit] Complexity
If reallocation happens, linear in the number of elements of the vector
after insertion; otherwise, linear in the number of elements inserted plus std::distance (pos, end()).
[edit] Exceptions
If an exception is thrown other than by
- the copy constructor of
T
,
- the move constructor of
T
,
- the copy assignment operator of
T
,
- the move assignment operator of
T
,
- any
InputIt
operation,
these functions have no effect (strong exception safety guarantee).
If an exception is thrown when inserting a single element at the end, and T
is CopyInsertable into *this or std::is_nothrow_move_constructible <T>::value is true, 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.
[edit] Example
#include <iostream> #include <iterator> #include <string_view> #include <vector> namespace stq { void println(std::string_view rem, const std::vector <int>& 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::vector <int> 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); // pos no longer valid, get a new one: pos = c1.begin(); std::vector <int> 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:
1. [100, 100, 100] 2. [200, 100, 100, 100] 3. [300, 300, 200, 100, 100, 100] 4. [300, 300, 400, 400, 200, 100, 100, 100] 5. [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100] 6. [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 specified for overload (3) | specified for all overloads |
LWG 406 | C++98 | the strong exception guarantee also applied if the exception is thrown by an InputIt operation
|
no guarantee in this case |