In: Computer Science
#include<vector>
#include<iostream>
using namespace std;
void println(const vector<int>& v)
{
for (int x : v)
cout << x << " ";
cout << endl;
}
void println(const vector<string>& v)
{
for (const string& x : v)
cout << " "<< x
<< " ";
cout << endl;
}
int main()
{
vector<int> v0;
cout << "An empty vector of integers: ";
println(v0);
vector<int> v1(3);
cout << "A vector with three "
<< "value−initialized
integers: ";
println(v1);
vector<string> v2(0);
cout << "A vector with three empty "
<< "strings: ";
println(v2);
vector<int> v3(3, 17);
cout << "A vector with three 17’s: ";
println(v3);
vector<int> v4(v3);
cout << "A copy of the previous vector: ";
println(v4);
v4.front() = 1;
v4.back() = 23;
cout << "The last vector with its first "
<< "and last elements changed
to 1 "
<< "and 23: ";
println(v4);
Create a function append(v1, v2) that adds a copy of all the elements of vector v1 to the end of vector v2. The arguments are vectors of integers. Write a test driver
#include<vector>
#include<iostream>
using namespace std;
void println(const vector<int>& v)
{
for (int x : v)
cout << x << " ";
cout << endl;
}
void println(const vector<string>& v)
{
for (const string& x : v)
cout << " "<< x << " ";
cout << endl;
}
void append(vector<int> v1,vector<int>
&v2)
{
for(int i=0;i<v1.size();i++)
{
v2.push_back(v1[i]);
}
}
int main()
{
vector<int> v0;
cout << "An empty vector of integers: ";
println(v0);
vector<int> v1(3);
cout << "A vector with three "
<< "value−initialized integers: ";
println(v1);
vector<string> v2(0);
cout << "A vector with three empty "
<< "strings: ";
println(v2);
vector<int> v3(3, 17);
cout << "A vector with three 17’s: ";
println(v3);
vector<int> v4(v3);
cout << "A copy of the previous vector: ";
println(v4);
v4.front() = 1;
v4.back() = 23;
cout << "The last vector with its first "
<< "and last elements changed to 1 "
<< "and 23: ";
println(v4);
append(v3,v4);
cout<<"v4 after appending v3 to v4: ";
println(v4);
append(v4,v3);
cout<<"v3 after appending v4 to v3: ";
println(v3);
}