In C++, std::vector::insert() adds new elements to a std::vectorbefore 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
Call
What 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.