Scroll to navigation

std::vector::insert(3) C++ Standard Libary std::vector::insert(3)

NAME

std::vector::insert - std::vector::insert

Synopsis


iterator insert( iterator pos, const (until
T& value ); C++11)
(since
iterator insert( const_iterator pos, C++11)
const T& value ); (until
C++20)
constexpr iterator insert( (since
const_iterator pos, const T& value C++20)
);
(since
iterator insert( const_iterator pos, C++11)
T&& value ); (until
C++20)
constexpr iterator insert( (since
const_iterator pos, T&& value ); C++20)
void insert( iterator pos, size_type (until
count, const T& value ); C++11)
(since
iterator insert( const_iterator pos, C++11)
size_type count, const T& value ); (until
C++20)
constexpr iterator insert( (since
const_iterator pos, size_type count, (1) C++20)
const T& value );
template< class InputIt > (until
void insert( iterator pos, InputIt C++11)
first, InputIt last ); (2)
template< class InputIt > (since
C++11)
iterator insert( const_iterator pos, (3) (until
C++20)
InputIt first, InputIt last );
template< class InputIt >


constexpr iterator insert( (4) (since
const_iterator pos, C++20)


InputIt first, InputIt last );
(since
iterator insert( const_iterator pos, C++11)
std::initializer_list<T> ilist ); (until
(5) C++20)
constexpr iterator insert( (since
const_iterator pos, C++20)
std::initializer_list<T> ilist );


Inserts elements at the specified location in the container.


1-2) inserts value before pos
3) inserts count copies of the value before pos
4) inserts elements from range [first, last) before pos.


This overload has the same effect as overload (3) if InputIt is an (until C++11)
integral type.
This overload participates in overload resolution only if InputIt
qualifies as LegacyInputIterator, to avoid ambiguity with the overload (since C++11)
(3).


The behavior is undefined if first and last are iterators into *this.
5) inserts elements from initializer list ilist before pos.


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 the end() iterator
value - element value to insert
count - number of elements to insert
the range of elements to insert, can't be
first, last - iterators into container for which insert is
called
ilist - initializer list to insert the values from

Type requirements


-
T must meet the requirements of CopyAssignable and CopyInsertable in order to use
overload (1).
-
T must meet the requirements of MoveAssignable and MoveInsertable in order to use
overload (2).
-
T must meet the requirements of CopyAssignable and CopyInsertable in order to use
overload (3).
-
T must meet the requirements of EmplaceConstructible in order to use overload (4,5).
-
T must meet the requirements of MoveAssignable and MoveInsertable in order to use
overload (4). required only if InputIt satisfies LegacyInputIterator but not
LegacyForwardIterator. (until C++17)
-
T must meet the requirements of Swappable, MoveAssignable, MoveConstructible and
MoveInsertable in order to use overload (4,5). (since C++17)

Return value


1-2) Iterator pointing to the inserted value
3) Iterator pointing to the first element inserted, or pos if count==0.
4) Iterator pointing to the first element inserted, or pos if first==last.
5) Iterator pointing to the first element inserted, or pos if ilist is empty.

Complexity


1-2) Constant plus linear in the distance between pos and end of the container.
3) Linear in count plus linear in the distance between pos and end of the container.
4) Linear in std::distance(first, last) plus linear in the distance between pos and
end of the container.
5) Linear in ilist.size() plus linear in the distance between pos and end of the
container.

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

// 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

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 a std::insert_iterator of type inferred from the argument
(function template)

2022.07.31 http://cppreference.com