std::vector insert() method
- since C++20
- since C++11
- until C++11
// prism-push-types:const_iterator,iterator,size_type
// (1) Non const version only
constexpr iterator insert( const_iterator pos, T const& value );
// (2) Non const version only
constexpr iterator insert( const_iterator pos, T&& value );
// (3) Non const version only
constexpr iterator insert( const_iterator pos, size_type count, T const& value );
// (4) Non const version only
template< class InputIt >
constexpr iterator insert( const_iterator pos, InputIt first, InputIt last );
// (5) Non const version only
constexpr iterator insert( const_iterator pos, std::initializer_list<T> ilist );
// prism-push-types:const_iterator,iterator,size_type
// (1) Non const version only
iterator insert( const_iterator pos, T const& value );
// (2) Non const version only
iterator insert( const_iterator pos, T&& value );
// (3) Non const version only
iterator insert( const_iterator pos, size_type count, T const& value );
// (4) Non const version only
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
// (5) Non const version only
iterator insert( const_iterator pos, std::initializer_list<T> ilist );
// prism-push-types:iterator,size_type
// (1) Non const version only
iterator insert( iterator pos, T const& value );
// (3) Non const version only
void insert( iterator pos, size_type count, T const& value );
// (4) Non const version only
template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last );
Inserts elements at the specified location in the container.
-
(1-2) Inserts
value
beforepos
. -
(3) Inserts
count
copies of thevalue
beforepos
. -
(4) Inserts elements from range [ first, last ) before
pos
.- since C++11
- until C++11
This overload participates in overload resolution only if
InputIt
qualifies asLegacyInputIterator
to avoid ambiguity with the overload (3).This overload has the same effect as overload (3) if
InputIt
is an integral type. -
(5) Inserts elements from initializer list
ilist
beforepos
.
Causes reallocation if the new size()
is greater than the old capacity()
.
If the new size()
is greater than capacity()
, all iterators and references are invalidated.
Otherwise, only the iterators and references before the insertion point remain valid.
The past-the-end iterator is also invalidated.
Parameters
pos
- iterator before which the content will be inserted. pos may be theend()
iteratorvalue
- element value to insertcount
- number of elements to insertfirst
,last
- the range of elements to insert, can't be iterators into container for which insert is calledilist
- initializer list to insert the values from
Type requirements
T
is the container's element type in all the requirements below.
-
(1)
T
must meet the requirements ofCopyAssignable
andCopyInsertable
. -
(2)
T
must meet the requirements ofMoveAssignable
andMoveInsertable
. -
(3)
T
must meet the requirements of CopyAssignable andCopyInsertable
. -
(4,5)
T
must meet the requirements ofEmplaceConstructible
-
- since C++17
- until C++17
(4-5)
T
must meet the requirements ofSwappable
,MoveAssignable
,MoveConstructible
andMoveInsertable
.(4) If
InputIt
satisfiesLegacyInputIterator
:T
must meet the requirements ofMoveAssignable
andMoveInsertable
.
Return value
- (1-2) Iterator pointing to the inserted value.
- (3) Iterator pointing to the first element inserted, or
pos
ifcount == 0
. - (4) Iterator pointing to the first element inserted, or
pos
iffirst == last
. - (5) Iterator pointing to the first element inserted, or
pos
ifilist
is empty.
Complexity
- (1-2) Constant plus linear in the distance between
pos
and end of the container - O(std::distance(pos, end())). - (3) Linear in
count
plus linear in the distance betweenpos
and end of the container - O(count + std::distance(pos, end())). - (4) Linear in
std::distance(first, last)
plus linear in the distance betweenpos
and end of the container - O(std::distance(first, last) + std::distance(pos, end())). - (5) Linear in
ilist.size()
plus linear in the distance betweenpos
and end of the container - O(ilist.size() + std::distance(pos, end())).
Exceptions
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
, there are no effects (strong exception guarantee).
Example
#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);
}
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
Hover to see the original license.