C++

Append Vector to Vector C++

Just like arrays, vectors in C++ are used to hold multiple values belonging to the same data type-together. For example, we can have string, integer, float, double, etc., type vectors in C++. Whenever we use a vector to store our data, we need to be aware of all the operations we can perform on these vectors. One such operation is the “append” operation, i.e., appending a vector to another. By appending a vector to another, we literally mean joining one vector without losing or overwriting any data. This tutorial will discuss the procedure of appending a vector to another vector in C++ in Ubuntu 20.04.

Appending Vector to Vector in C++ in Ubuntu 20.04:

Since vector is a very important data structure in C++, we expect to perform all the different operations on vectors with the other data structures. While dealing with strings, we come across such functions using which we can easily append one string to another, i.e., the ending point of the first string is joined with the starting point of the second string. Suppose you have two strings, “Hello” and “World”. When we append these two strings together, then we will get the string “HelloWorld”.

Similarly, you might want to append two vectors together. The basic concept of appending two vectors is the same as appending one string to another. However, the process of appending a vector to a vector is different from appending a string to a string. The function of C++ that is used to append a vector to another is known as the “insert” function. The syntax of the “insert” function is given below:

V1.insert(V1.endValue(), V2.startValue(), V2.endValue());

The “insert” function is always called with the first vector, “V1”. This function accepts three arguments. “V1.endValue()” refers to the endpoint or last value of the first vector from where we need to start appending the second vector. “V2.startValue()” refers to the starting point or first value of the second vector, which must be joined with the last value of the first vector. “V2.endValue()” refers to the endpoint or last value of the second vector, i.e., the point till you need to append the two vectors. You will get more clarity regarding the usage of this function by going through the following two examples of appending a vector to another in C++ in Ubuntu 20.04.

Example # 1: Appending an Integer Vector to an Integer Vector in C++:

In this example, we teach you how to append an integer vector to another integer vector in C++ in Ubuntu 20.04. The C++ code for this particular example is as follows:

In this code, we have included the “vector” header file along with the “iostream” header file so that we can easily use the vectors in C++. Then, we have defined two different vectors of integer type named “vect1” and “vect2” and have assigned them to the five different values each. After that, we have printed the values of these two vectors on the terminal by using the “for” loops. Then, we have used the statement “vect1.insert(vect1.end(), vect2.begin(), vect2.end())” for appending the first integer vector to the other. The “insert” function in C++ takes three arguments, i.e., the last value of the first vector, the first value of the second vector, and the last value of the second vector. After appending the second vector to the first, we have again used a “for” loop to display the appended values of these vectors on the terminal.

To compile this code for checking if it contains any errors or not, we have executed the command shown below:

$ g++ AppendVector.cpp –o AppendVector

Then, for executing this code to find out if our integer vectors have been appended successfully or not, we have used the following command:

$ ./AppendVector

After executing this code, the values of both the vectors separately and the appended values of both the vectors were displayed on the terminal, as shown in the image below. This shows that the second integer vector has been appended successfully to the first integer vector.

Example # 2: Appending a String Vector to a String Vector in C++:

In this example, we teach you how to append a string vector to another string vector in C++ in Ubuntu 20.04. The C++ code for this particular example is as follows:

In this code, we have included the “vector” and “string” header files along with the “iostream” header file so that we can easily use the vectors and strings in C++. Then, we have defined two different vectors of string type named “vect1” and “vect2” and assigned them two different values each. After that, we have printed the values of these two vectors on the terminal by using the “for” loops. Then, we have used the statement “vect1.insert(vect1.end(), vect2.begin(), vect2.end())” for appending the first string vector to the other. The “insert” function in C++ takes three arguments, i.e., the last value of the first vector, the first value of the second vector, and the last value of the second vector. After appending the second vector to the first, we have again used a “for” loop to display the appended values of these vectors on the terminal.

After executing this code, the values of both the vectors separately and the appended values of both the vectors were displayed on the terminal, as shown in the image below. This shows that the second string vector has been appended successfully to the first string vector.

Can Two Vectors with Different Data Types be Appended Together in C++?

Once you have learned how easy it is to append one vector to another in C++, the next question that might arise in your mind is whether two vectors with different data types can be appended together or not? For example, a string vector can be appended to an integer vector or not. The answer to this question is a “NO,” i.e., two vectors having different data types cannot be appended together since doing this always leads to compilation errors. Therefore, the two vectors must be appended to have the same data types.

Conclusion:

With the help of this guide, we wanted to explain to you the process of appending a vector to another vector in C++ in Ubuntu 20.04. We also wanted to elaborate that conceptually, appending a vector to another is similar to appending a string to another; however, achieving these goals is pretty much different from each other. To highlight this difference, we shared the syntax of the function to append one vector to another in C++. After that, with the help of two different examples, we showed you how you could append a vector to another. Hopefully, after understanding these examples, you will quickly grasp the method of appending a vector of any data type to another vector of the same data type in C++.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.