| by Arround The Web | No comments

Appending of Vector to Vector in C++

The vectors in C++ programming are well known as containers that can store in them the elements that are similar in their data types. Vectors are more or less similar to the arrays, but the vectors’ size is dynamic which means that the size of the vector can be changed during the execution phase i.e. we may insert some new elements to the vector or add another vector to the same vector without overwriting the previous vector. This process is called “appending of vector to vector”.

This article helps us in learning to append one or multiple vectors to a vector.

Syntax:
There are multiple methods to append two vectors. Their syntax may vary accordingly. In this article, we will implement the following two methods:

$ vec1.insert (vector1.end (), vector2.begin(), vector2.end ())
$ std :: copy (vector2.begin(), vector2.end (), std::back_inserter(vector1));

Let’s implement each of the previously-described methods one by one on different examples and observe their output.

Example 1: Using the Vector.Insert() to Append Two Vectors in C++
“Vector.insert()” is the method to append two vectors and is most frequent in practice. In syntax, we always call this method “insert” with the first available vector. Then, we pass three input arguments to this function and the very first argument includes the first vectors’ end value. After which, we append the second vector. The second argument specifies the starting value or the first position of the second vector which is needed to be appended with the first vector. The last argument consists of the last value of the second vector which is the final element of the second vector, i.e. the point until the two vectors should be appended.

To become clearer on this idea, we perform an example on the dummy data. We append the two vectors, each consisting of the elements with the “integer” data type. To run the code in a C++ compiler, we first import the libraries to work with the vectors and provide them with input and output. For this very purpose, include the “<iostream >” and the “” header files and add the “namespace std” to make sure that we use everything in the program within the namespace standard.

In the main function with the “integer” return type, we create two vectors named “vector1” and “vector2” respectively, each having three integer values. At this point, we call the insert function with the prefix of the first vector as “vector1.insert()” and pass the first value “vector1.end ()” to the input arguments of this function where the vector1 ends, the second value “vector2.begin()” from where the vector2 starts, and the third value “vector2.end ()” is the point or element until we want to append the second vector with the first vector.

Now, the second vector is appended to the first vector. To display the results, we use the “for loop” and end its iterators at the end of the “vector1” to traverse through vector1. Then, print the results of the appended vector1. The code and the output for this example are attached in the following:

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> vector1 = {4, ,8,7};
    vector<int> vector2 = {4, 5, 6};
    vector1.insert(vector1.end (), vector2.begin(), vector2.end ());
    for (int i: vector1) {
        cout << i << " ";
    }
    return 0;
}

Example 2: Using the Std::Copy Method to Append Two Vectors
This example illustrates how to append two vectors, both having the same data type by the implementation of the “std:: copy ()” method. This function is from the standard library and is used whenever we are required to copy the data from one vector to another one. This function takes in only three arguments as its input. The first argument describes the beginning point of that vector which we want to copy to another vector. The second input argument is the point where the vector (which needs to be copied) ends. And the final argument is also a function named “back_inserter ()”. Then, we pass the first vector to the input of this function. After which, we want to append the second vector.

To understand this “std::copy” function, we run an example code in the C++ compiler. To write that code, we let the compiler allow us to take an input and output and the declaration of the vectors by including the header files like and . Then, we use the “namespace std” to work with the standard library. After declaring the main function with the return type as “integer”, we move forward by creating two vectors “v1” and “v2”, both having the same number of elements and data types as “3” and “int”, respectively.

Now, we simply call the “std:: copy ()” function and pass the starting point, the ending point of the vector2 as the first two input arguments, and pass the vector1 to the third argument. This is because we want to append vector2 to the end of vector1. The “std::copy()” function does this job by copying the second vector to the end of the first vector. The appended vector1 is then displayed using the “for loop” and run its iteration until the end of the v1. Then, we simply print the appended vector. We attached the following snippet and the code for this example:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main () {
    vector<int> v1 = {1, 3, 3};
    vector<int> v2 = {4, 9, 6};
    std::copy (v2.begin(), v2.end (), std::back_inserter(v1));
    for (int i: v1) {
        cout << i << " ";
    }
    return 0;
}

Example 3: Appending of Two String Vectors
The previous examples depicted how we can append the vectors both having the elements as integers. Let’s make an example where we use the first method to append the vectors and append two string vectors. We create the two string vectors with the names “strv1” and “strv2”. To define these string vectors, we first have to include the <iostream>, <vector>, and <string> header files. In the main function, the two string vectors are created where each string has its elements as names.

Now, we call the “strv1.insert ()” function and pass the end value of “strv1”, the beginning value of the “strv2”, and the end value of the “strv2” as arguments of the function to append strv2 to strv1. Then, the output of the strv1 is displayed using the “for loop” where the iterator of the loop ends at the end of the strv1. We mentioned the code for this example along with the snippet of its output in the following:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main () {
    vector<string> strv1= {"Apple", "Kite", "fish"};
    vector<string> strv2 = {"iron", "chair"};
    strv1.insert(strv1.end (), strv2.begin(), strv2.end ());
    for (int i=0; i<strv1.size(); i++) {
        cout << strv1.at(i) << "; ";
    }
    cout<<endl;
    return 0;
}

Conclusion

This article depicts the various methods of appending two vectors with the same data type, either “integer” or as “string”. Appending of the vectors is done using two different methods – “vector.insert()” and the “std::copy()”. We implemented three different examples and the results of these three examples are shown. All these techniques are presented in the easiest possible way to make them understandable.

Share Button

Source: linuxhint.com

Leave a Reply