SoFunction
Updated on 2025-04-12

Summary of the usage of insert function of C++ vector

In C++,std::vectoris a dynamic array that provides flexible memory management and rich member functions.insertThe function isstd::vectorA very useful member function provided for inserting an element or another range of elements at a specified location.

std::vector::insertUsage

1. Insert a single element

iterator insert(const_iterator pos, const T& value);

pos: Iterator for inserting position (pointing to the previous element of the position to be inserted).

value: The element to be inserted.

2. Insert multiple identical elements

void insert(const_iterator pos, size_type count, const T& value);

pos: Iterator for insertion position.

count: The number of elements to be inserted.

value: The element to be inserted.

3. Insert an element in a range

template <class InputIterator>
void insert(const_iterator pos, InputIterator first, InputIterator last);

pos: Iterator for insertion position.

first: The starting iterator of the range.

last: End iterator for range.

4. Insert elements in the initialization list (C++11 and above)

void insert(const_iterator pos, initializer_list<T> ilist);

pos: Iterator for insertion position.

ilist: Initialize the list.

Sample code

Example 1: Insert a single element

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
int main() {
    vector&lt;int&gt; vec = {1, 2, 3, 4, 5};
    (() + 2, 99); // Insert 99 at index 2    for (int num : vec) {
        cout &lt;&lt; num &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    return 0;
}

Output:

1 2 99 3 4 5

Example 2: Insert multiple identical elements

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
int main() {
    vector&lt;int&gt; vec = {1, 2, 3, 4, 5};
    (() + 2, 3, 99); // Insert 3 99s at index 2    for (int num : vec) {
        cout &lt;&lt; num &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    return 0;
}

Output:

1 2 99 99 99 3 4 5

Example 3: Insert an element in a range

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
int main() {
    vector&lt;int&gt; vec1 = {1, 2, 3, 4, 5};
    vector&lt;int&gt; vec2 = {99, 100, 101};
    (() + 2, (), ()); // Insert all elements of vec2 at index 2    for (int num : vec1) {
        cout &lt;&lt; num &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    return 0;
}

Output:

1 2 99 100 101 3 4 5

Example 4: Insert an element in the initialization list

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
int main() {
    vector&lt;int&gt; vec = {1, 2, 3, 4, 5};
    (() + 2, {99, 100, 101}); // Insert element in the initialization list at index 2    for (int num : vec) {
        cout &lt;&lt; num &lt;&lt; " ";
    }
    cout &lt;&lt; endl;
    return 0;
}

Output:

1 2 99 100 101 3 4 5

Things to note

Iterator failed

The insertion operation invalidates all iterators after the insertion point.If you need to continue using the iterator after insertion, it is recommended to re-get it.

For example:

auto it = () + 2;
(it, 99);
it = () + 2; // Retrieve the iterator

performance

The time complexity of the insertion operation is O(n), where n is the number of elements after the insertion point. This is because the insertion operation may require moving all elements after the insertion point.

Memory allocation

If the insertion operation causesvectorInsufficient capacity,vectorMemory will be reallocated automatically, which may cause all iterators to fail.

Summarize

std::vector::insertIt is a very flexible function that can be used to insert a single element, multiple identical elements, a range of elements, or an initialization list at a specified location. By reasonable useinsertFunctions that can be operated easilyvectorcontent.

This is the article about the usage of insert function of C++ vector. For more information about insert function of C++ vector, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!