C++ std::vector insert() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

In C++, std::vector::insert() adds new elements to a std::vector before a position that you give as an iterator. Everything from that position onward moves one place further back to make room.

You can insert one value, several copies of a value, a range from another container, or a braced list. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

The first argument is always the position (an iterator). The rest describes what to add.

C++
v.insert(v.begin() + 1, 20);                // one value
v.insert(v.begin() + 1, 3, 0);              // three zeros
v.insert(v.end(), v.begin(), v.end());      // a range
v.insert(v.begin(), {1, 2});                // a list
CallWhat it does
v.insert(pos, value)Adds one copy of value before pos.
v.insert(pos, count, value)Adds count copies of value before pos.
v.insert(pos, first, last)Adds every element of the range [first, last) before pos.
v.insert(pos, {1, 2})Adds the values of the braced list before pos.

Inserting at end() is the fast case on a vector. Prefer push_back() there. Inserting at begin() shifts every element and is slow.

Library declarations
C++
iterator insert(const_iterator pos, const T& value);
iterator insert(const_iterator pos, T&& value);
iterator insert(const_iterator pos, size_type count, const T& value);

template<class InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last);

iterator insert(const_iterator pos, std::initializer_list<T> ilist);

Parameters

ParameterDescription
posRequired. An iterator. New elements are placed before it. Use d.end() to add at the back.
valueThe value to add (one-value and count forms).
countHow many copies of value to add.
first, lastA range [first, last) from another container. It must not point into this same vector.
ilistA braced list such as {1, 2}.

Return Value

Return ValueDescription
IteratorAn iterator to the first inserted element. If nothing was inserted, it is pos.

C++ Examples

Example 1 — Insert One Value

Put 20 between 10 and 30.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {10, 30, 40};

    v.insert(v.begin() + 1, 20);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. v.begin() + 1 points to 30, the element at index 1.
  2. insert() puts the new value 20 before that element.
  3. 30 and 40 move one place back, so the vector reads 10 20 30 40.

Example 2 — Insert Several Copies

Add three zeros in one call.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 5};

    v.insert(v.begin() + 1, 3, 0);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << v.size() << ")\n";
    return 0;
}

How It Works

  1. The vector begins with two elements, 1 and 5.
  2. insert(d.begin() + 1, 3, 0) adds 3 copies of 0 before the element 5.
  3. The size grows from 2 to 5, because three new elements were added.

Example 3 — Insert a Range from a Vector

Copy all elements of a vector to the back of the vector.

C++
#include <vector>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> extra = {7, 8, 9};
    std::vector<int> v = {1, 2};

    v.insert(v.end(), extra.begin(), extra.end());

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. extra.begin() and extra.end() describe the whole vector as a range.
  2. v.end() is the position, so the copied values are added after the last element of the vector.
  3. The vector is not changed. The vector now holds 1 2 7 8 9.

Frequently Asked Questions

It adds one or more new elements before a position in the vector and returns an iterator to the first new element.
Before. insert(d.begin(), x) puts x at the front, and insert(d.end(), x) puts x at the back.
Only near the two ends. In the middle, the elements on one side must move, so it takes linear time.
Insertion can invalidate all old iterators. The returned iterator is valid and points to the first inserted element.
push_back(x) always adds at the back and is the simplest call. insert() lets you choose any position and can add many elements at once.

Next: insert_range()

Insert a whole range at any position (C++23) with std::vector.

insert_range() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful