1.Write a function concat vec to input two vectors and return a vector with the
ID: 3912564 • Letter: 1
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, 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
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
#include<vector>
using namespace std;
//making concat_vec a template function so that it'll work for any type of vectors
template <class T>
vector<T> concat_vec(const vector<T> &vs1, const vector<T> &vs2){
//creating a result vector to store all elements of vs1 and vs2
vector<T> resultVector;
//adding all vs1 elements
for(int i=0;i<vs1.size();i++){
resultVector.push_back(vs1[i]);
}
//adding all vs2 elements
for(int i=0;i<vs2.size();i++){
resultVector.push_back(vs2[i]);
}
return resultVector;
}
int main(){
//creating a vector
vector<int> v;
//adding some elements
v.push_back(1);
v.push_back(2);
v.push_back(3);
//making concatenated vector using same vector
vector<int> concatenated=concat_vec(v,v);
//displaying the concatenated vector
for(int i=0;i<concatenated.size();i++){
cout<<concatenated[i]<<" ";
}
cout<<endl;
//answer for question 2
//creating a vector of vector of int type
vector<vector<int> > vv;
//creating 3 int vectors
vector<int> vec1;
vector<int> vec2;
vector<int> vec3;
//adding values to each vectors
vec1.push_back(1);
vec2.push_back(2);
vec2.push_back(3);
vec3.push_back(4);
vec3.push_back(5);
vec3.push_back(6);
vec3.push_back(7);
//adding vectors to the vector of vectors
vv.push_back(vec1);
vv.push_back(vec2);
vv.push_back(vec3);
//looping and printing all the things
for (int i = 0; i < vv.size(); ++i) { // outer loop
vector<int> &tmp = vv[i]; // reference to vector
for (int j = 0; j < tmp.size(); ++j) { // inner loop
//print i, j, tmp[j], vv[i][j]
cout<<"i="<<i<<", j="<<j<<", tmp[j]="<<tmp[j]<<", vv[i][j]="<<vv[i][j]<<endl;
}
}
return 0;
}
/*OUTPUT*/
1 2 3 1 2 3
i=0, j=0, tmp[j]=1, vv[i][j]=1
i=1, j=0, tmp[j]=2, vv[i][j]=2
i=1, j=1, tmp[j]=3, vv[i][j]=3
i=2, j=0, tmp[j]=4, vv[i][j]=4
i=2, j=1, tmp[j]=5, vv[i][j]=5
i=2, j=2, tmp[j]=6, vv[i][j]=6
i=2, j=3, tmp[j]=7, vv[i][j]=7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.