How to add or insert elements to Vector in C++

C-plus-plus vector

In this article, we will learn different ways of how to add or insert elements to Vector in C++ with examples.

Ways to add or insert elements to Vector in C++


  • How to add or insert one element in a Vector?
  • How to add or insert multiple elements?
  • How to add or insert a element in Vector at specific index?
  • How to insert element at the front of a vector?
  • How to Insert element at the end of a vector?

Important Notes about vectors:

  • New element addition to vector will result in vector size growth at runtime.
  • The iterators which are in place before addition will get invalidated because size and sometime postions of elements gets changed after addition.
  • If continous memory is not available for growth then internally vector gets memory allocation done to new location where memory equal to vector’s new size is available in continous locations.
  • vectors are always stored in continous memory locations, so growing a vector size sometime results in this memory relocation overhead.

1. How to add or insert one element to Vector in c++?


To add an element to a vector we can use the push_back() function. This function adds the element to an existing vector.
If the vector is empty it will add it at the first index, but if the vector is not empty it will add the new element at the
end.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec;

    intvec.push_back(1);

    /* Insert next elements */
    for (int i = 1; i < 5; ++i)
        intvec.push_back(i + 1);

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;

    return 0;
}

Output

1
2
3
4
5

2. How to add or insert a element in Vector at specific index ?


STL Vector class provides the insert() function to insert elements in a vector at a specific index. In this example, we are going to use the insert() function to insert an element in a vector.

Syntax

vector.insert(const_iterator pos, const value_type& val);
  • pos : Where pos is the index position where you would like to insert your element and val is the item that you want to insert.
  • insert() function returns the updated vector iterator and now the vector contains the inserted element at the requested index.

Let us understand this with a code example

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };

  //5th Location =  0             + 5
    auto Pos = intvec.begin() + 5;

    auto Iter = intvec.insert(Pos, 6);

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;

   

   //2nd Location =  0       + 2
    auto Itero = strvec.insert(strvec.begin() + 2, "Dravid");

    for (int i = 0; i < strvec.size(); ++i)
        cout << strvec[i] << endl;

    return 0;
}

Output

1
2
3
4
5
6
Sachin
Kohli
Dravid
Dhoni

3. How to add or insert multiple elements?


In this example, we are going to learn about multiple elements insertion in a vector. Here also we can make use of the
insert() function. insert() function has the overloaded version that allows us to insert multiple elements in one go.

Let us first check the syntax:

vector.insert(const_iterator pos, InputIterator first, InputIterator last);

where arguments are:

  • pos : This is the starting position at which we want to insert these multiple elements.
  • first : This is the first index of the insertion elements.It is passed as an iterator.
  • last : This is the end index of the insertion elements.It is passed as an iterator.

Let us understand this syntax with some code examples where we will make use of this function.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<int> intvec2{ 6, 7, 8, 9, 10 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };
    vector<string> strvec2{ "Dravid", "Kumble", "Srinath" };

    //5th Location =  0             + 5
    auto Pos = intvec.begin() + 5;

    auto iter = intvec.insert(Pos, intvec2.begin(), intvec2.end());

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;


    //2nd Location =  0       + 2
    auto Itero = strvec.insert(strvec.begin() + 2, strvec2.begin(), strvec2.end());

    for (int i = 0; i < strvec.size(); ++i)
        cout << strvec[i] << endl;

    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
Sachin
Kohli
Dravid
Kumble
Srinath
Dhoni

Another overloaded version of the insert() function allow us to directly specify the new elements by using the initializer list.


The syntax looks like this:

vector.insert(const_iterator pos, initializer_list list);

Parameters:

  • pos : This is the starting position at which we want to insert these multiple elements.
  • list : This is the initializer list that contains the new elements which we want to add to the vector.

Below is a code example:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    vector<string> strvec{ "Sachin", "Kohli", "Dhoni" };
 

    //5th Location =  0             + 5
    auto Pos = intvec.begin() + 5;

    auto iter = intvec.insert(Pos, { 6, 7, 8, 9, 10 });

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;


    //2nd Location =  0       + 2
    auto Itero = strvec.insert(strvec.begin() + 2, { "Dravid", "Kumble", "Srinath" });

    for (int i = 0; i < strvec.size(); ++i)
        cout << strvec[i] << endl;

    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
Sachin
Kohli
Dravid
Kumble
Srinath
Dhoni

4. How to insert element at the front of a vector ?


#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec{ 1, 2, 3, 4, 5 };

    auto iter = intvec.insert(intvec.begin(), 0);

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;

    return 0;
}


#include <vector>
#include <algorithm>

int main() {
    vector<int> intvec{ 1, 2, 3, 4, 5 };
    intvec.push_back(0);
    std::rotate(intvec.rbegin(), intvec.rbegin() + 1, intvec.rend());
}

Output


5. How to Insert element at the end of a vector ?


#include <iostream>
#include <vector>

using namespace std;

int main(void) {
    vector<int> intvec{ 1, 2, 3, 4, 5 };

    intvec.push_back(6);

    for (int i = 0; i < intvec.size(); ++i)
        cout << intvec[i] << endl;

    return 0;
}

Output

1
2
3
4
5
6