vector::insert() function in C++

In C++, the vector class provides a function insert(), to insert single or multiple elements before the element at the specified position. It increases, the size of vector by n. Where n is the number of elements inserted.

Table Of Contents

Introduction

Internally, a vectors stores the elements at contiguous memory locations. So, if we insert an element in between the other elements of vector then it will shift all the elements after it. This is called there is relocation of elements. It is a costly affair. So in ideal scenarios we should avoid using insert() function with vector.

There are different overloaded implementations of the insert() function i.e.

iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, size_type n, const value_type& val);
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
iterator insert (const_iterator position, value_type&& val);
iterator insert (const_iterator position, initializer_list<value_type> il);

Each overloaded implementation has a different purpose. Therefore, we will discuss each implementation one by one.

Advertisements

Using insert() function to insert single element in vector

To insert a single element in vector, we will use this overloaded version of insert() function

iterator insert (const_iterator position, const value_type& val);

Parameters:

It accepts 2 arguments,

  • position: The iterator pointing to the element at index position before which we need to insert element.
  • val: The element that needs to be inserted.

Returns:

  • It returns an iterator pointing to the newly inserted element.

Let’s see an example

Suppose we have a vector of 5 integers. Now we will insert a new integer before element at index position 2. As indexing in C++ starts from zero, so basically we will add the element before the 3rd element of the vector. But before that, we need to create an iterator pointing to the 3rd element of vector. For that we will just add 2 to the iterator pointing at the beginning of vector. Then we will pass that iterator, and the value to the insert() function. Let’s see the example,

#include <iostream>
#include <vector>
#include <algorithm>

int main ()
{
    // create a vector, and initialize with five integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15};

    // Create iterator pointing to element at index number 2
    auto pos = vectorObj.begin() + 2;

    // Insert an element at index position 2
    vectorObj.insert(pos, 100);

    for (const int& num: vectorObj) {
        std::cout<< num <<", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

11, 12, 100, 13, 14, 15,

We inserted a new element in the vector just before the element at index position 2. Now let’s move to the other overloading implementation of insert() function.

Using insert() function to insert multiple elements in the vector

For this we will use this overloaded version of insert() function

iterator insert (const_iterator position, size_type n, const value_type& val);

Parameters:

It accepts 3 arguments,

  • position: The iterator pointing to the element at index position before which we need to insert elements.
  • n: Number of elements that needs to be inserted.
  • val: Value that will be copied to the inserted elements.

Returns:

  • Iterator pointing to the first inserted element in the vector.

It will insert n elements before the element at the given index position. All the inserted elements will have the same value that is the value provided in the 3rd argument to the insert() function

Let us see an example,

Suppose we have a vector of 5 integers. Now we want to insert 4 elements at index position 2. For this we will call the insert() function and pass following arguments,
* Iterator pointing to element at index position 2.
* The integer 4, because we want to insert 4 elements
* The integer 11.

It will insert four 11’s from index position 2 onwards in the vector. Let us see the complete example,

#include <iostream>
#include <vector>
#include <algorithm>

int main ()
{
    // create a vector, and initialize with five integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15};

    // Create iterator pointing to element at index number 2
    auto pos = vectorObj.begin() + 2;

    // Insert four elements with 11 from index position 2
    vectorObj.insert(pos, 4, 11);

    for (const int& num: vectorObj) {
        std::cout<< num <<", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

11, 12, 11, 11, 11, 11, 13, 14, 15,

Here we inserted four value in the vector.

Using insert() function & initializer list to insert multiple elements in vector

For this we will use this overloaded implementation of vector function,

iterator insert (const_iterator position, initializer_list<value_type> il);

Parameters:

It accepts 2 arguments,

  • position: The iterator pointing to the element at index position before which we need to insert elements.
  • il: An initializer list contains the element that needs to be added

Returns:

  • Iterator pointing to the first inserted element in the vector.

Let’s see an example, where we have a vector of 5 integers and we will insert 4 integers at index position 2 in this vector. The size of vector will become 9 after this.

#include <iostream>
#include <vector>
#include <algorithm>

int main ()
{
    // create a vector, and initialize with five integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15};

    // Create iterator pointing to element at index number 2
    auto pos = vectorObj.begin() + 2;

    // Insert four elements from index position 2
    vectorObj.insert(pos, {101, 102, 103, 104});

    for (const int& num: vectorObj) {
        std::cout<< num <<", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

11, 12, 101, 102, 103, 104, 13, 14, 15,

Here we inserted four value in the vector.

Using insert() function & a range to insert multiple elements in vector

For this, we will use this overloaded implementation of the insert() function

template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);

Parameters:

It accepts 3 arguments,

  • position: The iterator pointing to the element at index position before which we need to insert elements.
  • first: Iterator pointing to the start of a range.
  • last: Iterative pointing at the end of a range.

Returns:

  • Iterator pointing to the first inserted element in the vector.

It inserts all the elements from the given range into the vector at the given index position. Let’s see an example,

In this example, we have a vector that contains 5 elements and another list that contains 4 elements. Now we will insert all the 4 elements of list into the vector at index position 2. For this we will call the insert() function of vector and pass the iterator pointing to element at index position 2. After that, we will pass the begin and end iterators of list. So the insert() function will insert all the elements of list at index position 2 in the vector.

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

int main ()
{
    // create a vector, and initialize with five integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15};

    std::list<int> listOfNumbers {101, 102, 103, 104};

    // Create iterator pointing to element at index number 2
    auto pos = vectorObj.begin() + 2;

    // Insert all elements of list into vector at index position 2
    vectorObj.insert(pos, listOfNumbers.begin(), listOfNumbers.end());

    for (const int& num: vectorObj) {
        std::cout<< num <<", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

11, 12, 101, 102, 103, 104, 13, 14, 15,

Here, we inserted all the elements of list into the vector at given index position.

Summary

In this article we went through all the implementations of insert() function of vector in C++. Another important point that we need to keep in mind is that the insertion will trigger the relocation of elements in the vector and that is a costly operation. So we should always avoid using insert() function of the vector, because the vector is not designed to insert the elements in between efficiently. Thanks for reading.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top