C++ Concatenate Vectors

In this article, we will learn how we can Concatenate Vectors in C++.

Vector Concatenation can be performed in many ways, we will discuss some of them.

Different Ways to Concatenate Vectors in C++

A and B are the input vector we want to concatenate.

AB is the vector which stores the concatenate of vector A and vector B.

Method 1: std::reserve and std::insert

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB.reserve(A.size() + B.size());
	AB.insert(AB.end(), A.begin(), A.end());
        AB.insert(AB.end(), B.begin(), B.end());
}
  1. First, we reserve memory for AB to store vector A and vector B using std::reserve.
  2. Then we push vector A elements to AB using std::insert.
  3. After that, we push vector B elements to AB.
  4. In this way, we concatenate A and B.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB.reserve(A.size() + B.size());
	AB.insert(AB.end(), A.begin(), A.end());
        AB.insert(AB.end(), B.begin(), B.end());
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 1 , 2 , 3 , 4 , 5};
	vector<int> B = { 9 , 11 , 14 ,15 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 1 2 3 4 5
Vector B: 9 11 14 15
Vector AB: 1 2 3 4 5 9 11 14 15

Method 2: std::insert

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB.insert(AB.end(), A.begin(), A.end());
        AB.insert(AB.end(), B.begin(), B.end());
}
  1. This method is slower than method 1.
  2. This is because in this method we are not reserving memory.
  3. Thus, multiple memory allocations may take place during runtime.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB.insert(AB.end(), A.begin(), A.end());
        AB.insert(AB.end(), B.begin(), B.end());
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 1 , 2 , 3 , 4 , 5};
	vector<int> B = { 9 , 11 , 14 ,15 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 1 2 3 4 5
Vector B: 9 11 14 15
Vector AB: 1 2 3 4 5 9 11 14 15

Method 3: assignment operator and std::insert

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB = A;
        AB.insert(AB.end(), B.begin(), B.end());
}
  1. This method is also slower than method 1.
  2. Multiple memory allocation may take place since AB may not be able to hold all elements of vector B.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	AB = A;
        AB.insert(AB.end(), B.begin(), B.end());
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 1 , 2 , 3 , 4 , 5, 7, 8};
	vector<int> B = { 9 , 11 , 14 ,15 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 1 2 3 4 5 7 8
Vector B: 9 11 14 15
Vector AB: 1 2 3 4 5 7 8 9 11 14 15

Method 4: std::copy

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	copy(A.begin(),A.end(),back_inserter(AB));
        copy(B.begin(),B.end(),back_inserter(AB));
}
  1. std::copy is used to copy elements.
  2. back_inserter(AB) tells std::copy to push the elements at the end of AB instead of overwriting.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
	copy(A.begin(),A.end(),back_inserter(AB));
        copy(B.begin(),B.end(),back_inserter(AB));
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 1 , 2 };
	vector<int> B = { 9 , 11 , 14 ,15 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 1 2
Vector B: 9 11 14 15
Vector AB: 1 2 9 11 14 15

Method 5: using push_back operation

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
        for(int i=0;i<A.size();++i)
                AB.push_back(A[i]);
        for(int i=0;i<B.size();++i)
                AB.push_back(B[i]);
}
  1. This method is very simple but the least efficient.
  2. This is because using multiple vector::push_back will cause multiple allocations in runtime.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
        for(int i=0;i<A.size();++i)
                AB.push_back(A[i]);
        for(int i=0;i<B.size();++i)
                AB.push_back(B[i]);
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 1 , 2 };
	vector<int> B = { 9 , 11 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 1 2
Vector B: 9 11
Vector AB: 1 2 9 11

Method 6: by overwriting

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
    AB = vector<int>(A.size() + B.size());
    for(int i=0;i<A.size();++i)
            AB[i] = A[i];
    for(int i=0;i<B.size();++i)
            AB[i+A.size()] = B[i];
}
  1. In this method, we first reserve memory to AB equal to A.size() + B.size(). Memory allocation take places only once.
  2. After that, we overwrite each index of vector AB with vector A values and then with vector B values.
#include<iostream>
#include<vector>
using namespace std;

void ConcatenateVector(vector<int> &A,vector<int> &B, vector<int> &AB){
    AB = vector<int>(A.size() + B.size());
    for(int i=0;i<A.size();++i)
            AB[i] = A[i];
    for(int i=0;i<B.size();++i)
            AB[i+A.size()] = B[i];
}

void printVector(vector<int> v){
	for(int i=0;i<v.size();++i)
		cout<<v[i]<<' ';
	cout<<endl<<endl;
}

int main() {

	vector<int> A = { 6, 8 , 9 };
	vector<int> B = { 9 , 11 };
	vector<int> AB;
	
	cout<<"Vector A: ";
	printVector(A);
	
	cout<<"Vector B: ";
	printVector(B);
	
	ConcatenateVector(A,B,AB);
	cout<<"Vector AB: ";
	printVector(AB);

}

Output

Vector A: 6 8 9
Vector B: 9 11
Vector AB: 6 8 9 9 11

What to Study Next?

  1. C++ Merge Sort Vector
  2. C++ Merge Two Sorted Vectors

Leave a Comment

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