| (1) | ||
| iterator insert( const_iterator pos, const T& value ); | (until C++20) | |
| constexpr iterator insert( const_iterator pos, const T& value ); | (since C++20) | |
| (2) | ||
| iterator insert( const_iterator pos, T&& value ); | (since C++11) (until C++20) | |
| constexpr iterator insert( const_iterator pos, T&& value ); | (since C++20) | |
| (3) | ||
| iterator insert( const_iterator pos, size_type count, const T& value ); | (until C++20) | |
| constexpr iterator
    insert( const_iterator pos, size_type count, const T& value ); | (since C++20) | |
| (4) | ||
| template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last ); | (until C++20) | |
| template< class InputIt > constexpr iterator insert( const_iterator pos, InputIt first, InputIt last ); | (since C++20) | |
| (5) | ||
| iterator insert( const_iterator pos, std::initializer_list<T> ilist ); | (since C++11) (until C++20) | |
| constexpr iterator insert( const_iterator pos,
                           std::initializer_list<T> ilist ); | (since C++20) | 
Inserts elements at the specified location in the container.
value before pos.count copies of the value before pos.[first, last) before pos. | This overload has the same effect as overload (3) if  | (until C++11) | 
| This overload participates in overload resolution only if  | (since C++11) | 
first and last are iterators into *this. 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 end() iterator is also invalidated.
| 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 which insert is called | 
| ilist | - | initializer list to insert the values from | 
| Type requirements | ||
| - Tmust meet the requirements of CopyAssignable and CopyInsertable in order to use overload (1). | ||
| - Tmust meet the requirements of MoveAssignable and MoveInsertable in order to use overload (2). | ||
| - Tmust meet the requirements of CopyAssignable and CopyInsertable in order to use overload (3). | ||
| - Tmust meet the requirements of EmplaceConstructible in order to use overload (4,5). | ||
| - Tmust meet the requirements of MoveAssignable and MoveInsertable in order to use overload (4). required only ifInputItsatisfies LegacyInputIterator but not LegacyForwardIterator. (until C++17) | ||
| - Tmust meet the requirements of Swappable, MoveAssignable, MoveConstructible and MoveInsertable in order to use overload (4,5). (since C++17) | ||
value.pos if count == 0.pos if first == last.pos if ilist is empty.pos and end of the container.count plus linear in the distance between pos and end of the container.std::distance(first, last) plus linear in the distance between pos and end of the container.ilist.size() plus linear in the distance between pos and end of the container.If an exception is thrown other than by.
T, | 
 | (since C++11) | 
T, | 
 | (since C++11) | 
InputIt operation, this function has no effect (strong exception guarantee).
| If an exception is thrown when inserting a single element at the end, and  | (since C++11) | 
#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
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 InputItoperation | no guarantee in this case | 
| (C++11) | constructs element in-place (public member function) | 
| adds an element to the end (public member function) | |
| creates a std::insert_iteratorof type inferred from the argument(function template) | 
    © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
    https://en.cppreference.com/w/cpp/container/vector/insert