std::vector::insert

From cppreference.com
< cpp‎ | container‎ | vector

(1)
iterator insert( iterator pos, const T& value );
(until C++11)
iterator insert( const_iterator pos, const T& value );
(since C++11)
iterator insert( const_iterator pos, T&& value );
(2) (since C++11)
(3)
void insert( iterator pos, size_type count, const T& value );
(until C++11)
iterator insert( const_iterator pos, size_type count, const T& value );
(since C++11)
(4)
template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last);
(until C++11)
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );
(since C++11)
iterator insert( const_iterator pos, std::initializer_list<T> ilist );
(5) (since C++11)

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 integral type. (until C++11)
This overload only participates in overload resolution if InputIt qualifies as InputIterator, to avoid ambiguity with the overload (3). (since C++11)
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.

Contents

[edit] Parameters

pos - iterator before which the content will be inserted. pos may be the end() iterator
value - element value to insert
first, last - the range of elements to insert, can't be 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 InputIterator but not ForwardIterator. (until C++17)
-
T must meet the requirements of Swappable, MoveAssignable, MoveConstructible and MoveInsertable in order to use overload (4,5). (since C++17)

[edit] 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.

[edit] 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.

[edit] 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 <vector>
 
void print_vec(const std::vector<int>& vec)
{
    for (auto x: vec) {
         std::cout << ' ' << x;
    }
    std::cout << '\n';
}
 
int main ()
{
    std::vector<int> vec(3,100);
    print_vec(vec);
 
    auto it = vec.begin();
    it = vec.insert(it, 200);
    print_vec(vec);
 
    vec.insert(it,2,300);
    print_vec(vec);
 
    // "it" no longer valid, get a new one:
    it = vec.begin();
 
    std::vector<int> vec2(2,400);
    vec.insert(it+2, vec2.begin(), vec2.end());
    print_vec(vec);
 
    int arr[] = { 501,502,503 };
    vec.insert(vec.begin(), arr, arr+3);
    print_vec(vec);
}

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

[edit] See also

(C++11)
constructs element in-place
(public member function)
adds elements to the end
(public member function)