Append Vector to Vector in C++
-
Use the
insert
Function to Append Vector to Vector in C++ -
Use
insert
Function to Add Elements in Vector in C++
This article will explain several methods of how to append a vector to another vector in C++.
Use the insert
Function to Append Vector to Vector in C++
The insert
method is a built-in function of the std::vector
container that can add multiple elements to the vector
objects. As the first example, we show how to append a given range from one vector
to another. If we specify three iterators as arguments, the insert
function will add elements from the last two arguments’ range before the iterator passed as the first parameter. Since we pass the end
iterator of the first vector
object in the following code example, the function is essentially doing appending of two vectors.
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
template<typename T>
void printVectorElements(vector<T> &vec)
{
for (auto i = 0; i < vec.size(); ++i) {
cout << vec.at(i) << "; ";
}
cout << endl;
}
int main() {
vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
vector<int> i_vec2 = {121, 321, 431, 531, 231, 651, 841};
cout << "i_vec1 : ";
printVectorElements(i_vec1);
i_vec1.insert(i_vec1.end(), i_vec2.begin(), i_vec2.end());
cout << "i_vec1 (inserted): ";
printVectorElements(i_vec1);
cout << endl;
return EXIT_SUCCESS;
}
Output:
i_vec1 : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (inserted): 12; 32; 43; 53; 23; 65; 84; 121; 321; 431; 531; 231; 651; 841;
Alternatively, we can specify the iterators using std::end
/std::begin
methods, implementing a more generic way of passing arguments to the insert
function:
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
template<typename T>
void printVectorElements(vector<T> &vec)
{
for (auto i = 0; i < vec.size(); ++i) {
cout << vec.at(i) << "; ";
}
cout << endl;
}
int main() {
vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
vector<int> i_vec2 = {121, 321, 431, 531, 231, 651, 841};
cout << "i_vec1 : ";
printVectorElements(i_vec1);
i_vec1.insert(std::end(i_vec1), std::begin(i_vec2), std::end(i_vec2));
cout << "i_vec1 (inserted): ";
printVectorElements(i_vec1);
cout << endl;
return EXIT_SUCCESS;
}
Use insert
Function to Add Elements in Vector in C++
Another common way of using the insert
method is to add a range of elements with a given value to the vector
. For example, we can insert zeros at the first 4 positions of the integer vector
. Note that the first argument is the position of the element before which the elements are added. The insert
method can be utilized even if the vector
elements are allocated manually using new
call;
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
template<typename T>
void printVectorElements(vector<T> &vec)
{
for (auto i = 0; i < vec.size(); ++i) {
cout << vec.at(i) << "; ";
}
cout << endl;
}
int main() {
vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
cout << "i_vec1 : ";
printVectorElements(i_vec1);
i_vec1.insert(i_vec1.begin(), 4, 0);
cout << "i_vec1 (inserted): ";
printVectorElements(i_vec1);
cout << endl;
return EXIT_SUCCESS;
}
Output:
i_vec1 : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (inserted): 0; 0; 0; 0; 12; 32; 43; 53; 23; 65; 84;
The insert
method can also be applied when two vectors of strings need to be concatenated. The following example demonstrates the given case with almost the same syntax as used with the integer vectors.
#include <iostream>
#include <vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
template<typename T>
void printVectorElements(vector<T> &vec)
{
for (auto i = 0; i < vec.size(); ++i) {
cout << vec.at(i) << "; ";
}
cout << endl;
}
int main() {
vector<string> str_vec1 = {"doordash", "dribble",
"renode", "xilinx"};
vector<string> str_vec2 = {"airbus", "sikorsky"};
str_vec1.insert(str_vec1.end(), str_vec2.begin(), str_vec2.end());
printVectorElements(str_vec1);
cout << endl;
return EXIT_SUCCESS;
}
Output:
doordash; dribble; renode; xilinx; airbus; sikorsky;