Question
1.Write a function concat vec to input two vectors and return a vector with the concatenated elements of the two vectors. vector concat_vec(const vector &vs1,
1.Write a function concat vec to input two vectors and return a vector with the concatenated elements of the two vectors. vector concat_vec(const vector &vs1, const vector &vs2); Example: 1. vs1 has data { "a", "b" }. 2. vs2 has data { "x", "y", "z" }. 3. Output vector has data { "a", "b", "x", "y", "z" }. Note the following: 1. The inputs arguments are const references to vectors. 2. Therefore the function cannot change them internally. 3. The function return value is a vector. 4. It is perfectly possible for the return type of a function to be a vector. Why not? Your function must work even if one or more inputs are empty. Your function must work even if both references are bound to the same object in the calling application. vector v, w; // (populate v) w = concat_vec(v,v); // both input references bound to same vector Your function will be tested with the following code. vector v; // (populate v) v = concat_vec(v,v); // all three are v // (print result) v = concat_vec(v,v); // call it twice! // (print result)
2. We can create a vector of vectors. Why not? Declare vv as a vector of vectors of type int. Also declare three vectors vec1, vec2, vec3 of type int. vector> vv; // vector of vectors vector vec1, vec2, vec3; Populate vec1, vec2, vec3 to hold the following data: 1. vec1 has length 1 and holds the value {1}. 2. vec2 has length 2 and holds the values {2, 3}. 3. vec3 has length 4 and holds the values {4, 5, 6, 7}. Populate vv to hold the following data: 1. vv[0] = vec1. 2. vv[1] = vec2. 3. vv[2] = vec3. Run a nested loop to print the data values. Do it as follows. 1. Run an outer loop for i = 0, . . . , vv.size() ? 1. This is obvious. 2. Reference to vector: declare a temporary reference variable inside the loop. vector &tmp = vv[i]; 3. Run an inner loop and print the values of i, j, tmp[j], vv[i][j] for j = 0, . . . , tmp.size() ? 1. 4. Note the double subscript vv[i][j]. 5. A vector of vectors is effectively a two-dimensional array. Your overall code should look like the following. for (int i = 0; i < vv.size(); ++i) { // outer loop vector &tmp = vv[i]; // reference to vector for (int j = 0; j < tmp.size(); ++j) { // inner loop //print i, j, tmp[j], vv[i][j] close all the loops
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started