std::vector::insert(3)
π std::vector::insert(3) π
okay bestie, welcome to the C++ Standard Libary β yes, this page is literally about std::vector::insert, and it's giving main-character energy. π
π NAME π
std::vector::insert - std::vector::insert
π Synopsis π
iterator insert( const_iterator pos, const T& value ); (1)
(constexpr since C++20)
iterator insert( const_iterator pos, T&& value ); (2) (since C++11)
(constexpr since C++20)
iterator insert( const_iterator pos, (3)
(constexpr since C++20)
size_type count, const T& value );
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, (4)
(constexpr since C++20)
InputIt last );
iterator insert( const_iterator pos, (5) (since C++11)
std::initializer_list<T> ilist );
(constexpr since C++20)
Inserts elements at the specified location in the container. π«Ά
- 1,2) Inserts
valuebeforepos. - 3) Inserts
countcopies ofvaluebeforepos. - 4) Inserts elements from range
[first, last)beforepos. - 5) Inserts elements from initializer list
ilistbeforepos.
For overload (4), bestie, listen:
- (until C++11) This overload has the same effect as overload (3) if
InputItis an integral type. - (since C++11) This overload participates in overload resolution only if
InputItqualifies asLegacyInputIterator, to avoid ambiguity with overload (3).
π¨ Iffirstandlastare iterators into*this, the behavior is undefined. Not cute, not allowed, no cap.
Also important, and I cannot stress this enough: 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. β¨
π Parameters π
posβ iterator before which the content will be inserted (posmay be theend()iterator).valueβ element value to insert.countβ number of elements to insert.first,lastβ the range of elements to insert, cannot be iterators into container for whichinsertis called.ilistβ initializer list to insert the values from.
π¦ Type requirements π¦
Tmust meet the requirements ofCopyAssignableandCopyInsertablein order to use overload (1).Tmust meet the requirements ofMoveAssignableandMoveInsertablein order to use overload (2).Tmust meet the requirements ofCopyAssignableandCopyInsertablein order to use overload (3).Tmust meet the requirements ofEmplaceConstructiblein order to use overloads (4,5).Tmust meet the requirements ofMoveAssignableandMoveInsertablein order to use overload (4). required only ifInputItsatisfiesLegacyInputIteratorbut notLegacyForwardIterator. (until C++17)Tmust meet the requirements ofSwappable,MoveAssignable,MoveConstructibleandMoveInsertablein order to use overloads (4,5). (since C++17)
πΈ Return value πΈ
- 1,2) Iterator pointing to the inserted value.
- 3) Iterator pointing to the first element inserted, or
posifcount == 0. - 4) Iterator pointing to the first element inserted, or
posiffirst == last. - 5) Iterator pointing to the first element inserted, or
posifilistis empty.
π₯ Complexity π₯
- 1,2) Constant plus linear in the distance between
posand end of the container. - 3) Linear in
countplus linear in the distance betweenposand end of the container. - 4) Linear in
std::distance(first, last)plus linear in the distance betweenposand end of the container. - 5) Linear in
ilist.size()plus linear in the distance betweenposand end of the container.
π 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) - any
InputItoperation,
these functions have no effect (strong exception safety guarantee). π‘οΈ
(since C++11) If an exception is thrown when inserting a single element at the end, and T is CopyInsertable 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.
π Example π
bestie, run this code:
// Run this code
#include <iostream>
#include <iterator>
#include <vector>
void print(int id, const std::vector<int>& container)
{
std::cout << id << ". ";
for (const int x : container)
std::cout << x << ' ';
std::cout << '\n';
}
int main ()
{
std::vector<int> c1(3, 100);
print(1, c1);
auto it = c1.begin();
it = c1.insert(it, 200);
print(2, c1);
c1.insert(it, 2, 300);
print(3, c1);
// `it` no longer valid, get a new one:
it = c1.begin();
std::vector<int> c2(2, 400);
c1.insert(std::next(it, 2), c2.begin(), c2.end());
print(4, c1);
int arr[] = {501, 502, 503};
c1.insert(c1.begin(), arr, arr + std::size(arr));
print(5, c1);
c1.insert(c1.end(), {601, 602, 603});
print(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
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. glow-ups but make it standards committee π
LWG 149, applied to C++98 β behavior as published: overloads (3) and (4) returned nothing; correct behavior: returns an iterator.LWG 247, applied to C++98 β behavior as published: the complexity was only specified for overload (3); correct behavior: specified for all overloads.LWG 406, applied to C++98 β behavior as published: the strong exception guarantee also applied if the exception is thrown by anInputItoperation; correct behavior: no guarantee in this case.
π See also π
emplaceβ constructs element in-place (C++11) (public member function)push_backβ adds an element to the end (public member function)inserterβ creates astd::insert_iteratorof type inferred from the argument (function template)
sign-off, bestie: source is http://cppreference.com, version/date is 2024.06.10, and this iconic page is std::vector::insert(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms