This tutorial will discuss how to insert multiple elements into vector in C++.

Insert Multiple Elements with Same value in Vector

In C++, to insert multiple elements between other elements in an existing vector, we can use an overloaded version of the insert() function. This version accepts three arguments:
1. An iterator pointing to the position where we want to insert the elements.
2. The number of elements that we want to insert.
3. The value of the elements to be inserted.

This will insert multiple elements with the same value into the vector. The number of elements to be inserted is specified by the second argument.

For instance, consider a vector of integers,

std::vector<int> vecObj = {11, 22, 33, 44, 55};

and we want to insert four elements with the value of 5 at the 3rd position (index 2) in the vector. We would call the insert() function with an iterator pointing to the 3rd position as the first argument, which we can obtain by calling the begin() function and adding 2 to it. As the second argument, we would pass 4 because we want to insert four elements, and as the third argument, we would pass 5, which is the value of the newly inserted elements.

The insert() function will create four copies of the given element and insert them at the specified position. It returns an iterator pointing to the first inserted element in the vector.

Here is an example code snippet:

std::vector<int> vecObj = {11, 22, 33, 44, 55};

// Get an iterator to third element in vector
auto it = vecObj.begin() + 2;

// Insert value 5 four times into the vector from index position 2
vecObj.insert(it, 4, 5);

After this operation, the contents of vecObj will be: 11, 22, 5, 5, 5, 5, 33, 44, 55,.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vecObj = {11, 22, 33, 44, 55};

    // Get an iterator to third element in vector
    auto it = vecObj.begin() + 2;

    // Insert value 5 four times into the vector from index position 2
    vecObj.insert(it, 4, 5);

    // Print the contents of vector
    for (const auto &num : vecObj)
    {
        std::cout << num << ", ";
    }
    std::cout << "n";

    return 0;
}

Output

11, 22, 5, 5, 5, 5, 33, 44, 55,

Insert Multiple Elements from Array into Vector

If you want to insert multiple elements into a vector, each with different values, you can use a different version of the insert() function. Suppose you have an array of integers and want to insert all the values from this array at the 3rd position (index 2) in the vector. You would call the insert function with the following arguments:

  1. An iterator pointing to the 3rd position in the vector.
  2. An iterator pointing to the start of the array.
  3. An iterator pointing to the end of the array.

This will insert all the elements from the array into the vector, starting at the 3rd position.

Here’s an example to illustrate:

std::vector<int> vecObj = {11, 22, 33, 44, 55};
int arr[] = {81, 82, 83, 94};

// Get an iterator to third element in vector
auto it = vecObj.begin() + 2;

// Insert all values from Array into the vector from index position 2
vecObj.insert(it, arr, arr + sizeof(arr)/sizeof(int));

After executing the above code, the contents of vecObj will be: 11, 22, 81, 82, 83, 94, 33, 44, 55,.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vecObj = {11, 22, 33, 44, 55};
    int arr[] = {81, 82, 83, 94};

    // Get an iterator to third element in vector
    auto it = vecObj.begin() + 2;

    // Insert value 5 four times into the vector from index position 2
    vecObj.insert(it, arr, arr + sizeof(arr)/sizeof(int));

    // Print the contents of vector
    for (const auto &num : vecObj)
    {
        std::cout << num << ", ";
    }
    std::cout << "n";

    return 0;
}

Output

11, 22, 81, 82, 83, 94, 33, 44, 55,

This is how you can insert multiple elements, each with different values, into a vector in C++.

Summary

Today, we learned how to insert multiple elements into vector in C++.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

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