Vector insert() in C++ STL (original) (raw)

The vector::insert() function in C++ STL is used to insert one or more elements at a specified position in a vector. It automatically shifts existing elements to create space for the new elements while preserving their relative order.

#include <bits/stdc++.h> using namespace std;

int main() { vector v = {1, 3, 7, 9};

// Insert element 8 at index 2
v.insert(v.begin() + 2, 8);

for (auto i : v)
    cout << i << " ";
return 0;

}

`

**Explanation

Syntax

The vector insert() is a member function of std::vector class defined inside ****** header file and have 3 implementations:

v.insert(pos, val); // Insert single element

v.insert(pos, n, val); // Insert multiple copies of an element

v.insert(pos, {val1, val2, ...}) // Insert list of elements

v.insert(pos, first, last); // Insert range of elements

**Parameters:

**Return Value: Returns an iterator pointing to the first inserted element.

Different Ways to Use vector::insert()

The vector::insert() function provides multiple overloads that allow inserting elements in different ways depending on the requirement. The following are the commonly used forms of insert().

Insert an Element at Given Index

The insert() function can be used to insert a single element at any valid position in a vector. All elements at and after the insertion position are shifted one place to the right.

**Syntax

v.insert(pos, val);

**Parameters:

**Return Value: Returns an iterator to the inserted element.

The insert() function is crucial for adding elements to a vector at specific positions.

C++ `

#include <bits/stdc++.h> using namespace std;

int main() { vector v = {1, 2, 4, 5};

// Insert element 3 at index 2
v.insert(v.begin() + 2, 3);

for (auto i : v)
    cout << i << " ";
return 0;

}

`

**Explanation

**Complexity Analysis

Insert Multiple Copies of an Element

The vector::insert() function can also be used to insert multiple copies of the same element at a specified position in a vector. All existing elements at and after the insertion position are shifted to accommodate the new elements.

**Syntax

v.insert(pos, n, val);

**Parameters:

**Return Value:

#include <bits/stdc++.h> using namespace std;

int main() { vector v = {1, 3, 4, 5};

// Inserting value 9, 3 times at index 2
v.insert(v.begin() + 1, 3, 2);

for (auto i : v)
    cout << i << " ";
return 0;

}

`

**Explanation

**Complexity Analysis

Insert a List of Elements

The vector::insert() function can insert multiple elements from an initializer list at a specified position in the vector. The inserted elements maintain the same order as they appear in the initializer list.

**Syntax

v.insert(pos, {val1, val2, ...});

**Parameters:

**Return Value:

#include <bits/stdc++.h> using namespace std;

int main() { vector v{1, 2, 3,};

// Inserting elements to v
v.insert(v.begin() + 3, {4, 5});

for (auto i : v)
    cout << i << " ";
return 0;

}

`

**Explanation

**Complexity Analysis

Insert a Range of Elements

The vector::insert() function can also insert a range of elements from another container or array into a vector at a specified position. The elements are inserted in the same order as they appear in the source range.

**Syntax

v.insert(pos, first, last);

**Parameters:

**Return Value:

#include <bits/stdc++.h> using namespace std;

int main() { vector v{1, 2, 3,}; list l{4, 5};

// Inserting all elements of l to v
v.insert(v.begin() + 3, l.begin(), l.end());

for (auto i : v)
    cout << i << " ";
return 0;

}

`

**Explanation

**Complexity Analysis

vector::insert() Vs vector::push_back()

Both vector::insert() and vector::push_back() are used to add elements to a vector, but they differ in terms of insertion position, flexibility, and performance.

Feature vector::insert() vector::push_back()
**Position Inserts at any specified position. Always appends at the end of the vector.
**Number of Elements Can insert single or multiple elements or a range. Inserts only one element at a time.
**Element Shifting Shifts elements after the insertion point. No shifting of elements (added at the end).
**Time Complexity O(n) O(1)