Insert a vector into another vector in C++

Hey! Are you are looking for the easiest way to insert a vector into another vector in C++?? Yes, you are at the right place this tutorial discusses all about the insertion of the vector into another vector. So let’s begin with a quick introduction.

Introduction

Vector is a part of STL (Standard Template Library). It can be considered similar to arrays. Both are used to store elements but there is a major difference, vectors have the capability of resizing themselves which is not present in arrays. As we already know arrays are of fixed size. So in other words we can say that vectors are dynamic arrays.

 

How to insert a vector into another vector in C++

This can be done using some simple steps:-

1) Header file:- The first step is to include the header file of the vector. So let’s see how to do that.

#include <vector>

 2) Declaration of vector:- The next step is the declaration of vectors. In our case, we are making a vector of characters. we name our vectors as vector1 and vector2.

vector<char> vector1{'a','b','c'};
vector<char> vector2{'x','y','z'};

3) Using insert() function:- Here we are using built-in function insert().It takes three parameters the first one – which points to the end of the vector in which you want to insert, the next is the vector that is being inserted, in our case we want our vector to insert from the beginning till the end which is the last parameter.

vector1.insert(vector1.end(),vector2.begin(),vector2.end());

4) Printing the vector:- The final step is to print our vector elements This can be done using for loop.

for(auto i:vector1)
{
   cout<<i<<" ";
}
cout<<endl;

Many of us already know why we are using auto here but for whom this term is new, let me explain, auto keyword automatically determines the type of variable that is being declared.

if you are not familiar with this shorter way no need to worry about that you can use the below-given method to print vector elements.

for (auto i = 0; i < vector1.size(); i++)
{
   cout << vector1.at(i) << " ";
}
cout << endl;

Here we will loop from starting to the size of the vector and will print the corresponding value. vector1.at()return the element that is present at that location.

Entire code

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    //Insert a vector into another vector in C++

    vector<char> vector1{'a','b','c'};
    vector<char> vector2{'x','y','z'};

    //Printing vector1

    cout<<"The elements of vector1 are :-"<<endl;

    for(auto i:vector1)
    {
        cout<<i<<" ";
    }
    cout<<endl;

   //Printing vector2

    cout<<"The elements of vector2 are :-"<<endl;

     for(auto i:vector2)
    {
        cout<<i<<" ";
    }
    cout<<endl;

   //insert function

    vector1.insert(vector1.end(),vector2.begin(),vector2.end());

    cout<<"********After inserting vector2 into vector1**********"<<endl;

    cout<<"The elements of vector1 are:-"<<endl;

    for(auto i:vector1)
    {
        cout<<i<<" ";
    }
    cout<<endl;
    return 0;
}

Output:-

The elements of vector1 are :-
a b c
The elements of vector2 are :-
x y z
********After inserting vector2 into vector1**********
The elements of vector1 are:-
a b c x y z

So here we come to the end of this tutorial.

Happy learning!

Leave a Reply

Your email address will not be published.