In: Computer Science
Please use c++
Consider the code on the next page. It creates a vector of five strings of vegetable names and you want to make the vector contain only vegetable names that you like. In the spaces designated, perform the following:
#include
#include
using namespace std;
void printVector(vector);
int main()
{
vector vegs;
vegs.push_back("Broccoli");
vegs.push_back("Celery");
vegs.push_back("Kale");
vegs.push_back("Tomato");
vegs.push_back("Carrot");
printVector(vegs);
cout << "Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable: " << endl;
printVector(vegs);
cout << "With the same iterator, point it to the beginning of the list, and erase the first item, ‘Broccoli’ (Because no one likes broccoli.): " << endl;
printVector(vegs);
cout << "Write two lines of code that will add two vegetable names that you DO like that isn't already on this list." << endl;
printVector(vegs);
return 0;
}
void printVector(vector v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl << endl;
}
Ans:-
(a.) vector<string>:: iterator itr; //
it will declare an iterator for vector
for(auto itr=vegs.begin();itr!=vegs.end();++itr) // loop to
iterate through vector using iterator
{
if(*itr=="Tomato") // check if the iterator points to the
"Tomato"
vegs.erase(itr); // remove the item using erase
function
}
(b.) itr=vegs.begin(); // now iterator points
to the begining of vector
vegs.erase(itr); // remove the item using erase
function
(c.) vegs.push_back("Cauliflower"); // it will add
vegetable cauliflower to the vector
vegs.push_back("Capsicum"); // it will add
vegetable capsicum to the vector
The whole updated code is shown below:-
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<string>);
int main()
{
vector<string> vegs;
vegs.push_back("Broccoli");
vegs.push_back("Celery");
vegs.push_back("Kale");
vegs.push_back("Tomato");
vegs.push_back("Carrot");
printVector(vegs);
cout << "Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable: " << endl;
vector<string>:: iterator itr; //it will declare an iterator for vector
for(auto itr=vegs.begin();itr!=vegs.end();++itr) //loop to iterate through vector using iterator
{
if(*itr=="Tomato") // check if the iterator points to the "Tomato"
vegs.erase(itr); //remove the item using erase function
}
printVector(vegs);
cout << "With the same iterator, point it to the beginning of the list, and erase the first item, ‘Broccoli’ (Because no one likes broccoli.): " << endl;
itr=vegs.begin(); //now iterator points to the begining of vector
vegs.erase(itr); //remove the item using erase function
printVector(vegs);
cout << "Write two lines of code that will add two vegetable names that you DO like that isn't already on this list." << endl;
vegs.push_back("Cauliflower"); // insert cauliflower to the list
vegs.push_back("Capsicum"); // insert capsicum to the list
printVector(vegs);
return 0;
}
void printVector(vector<string> v)
{
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl << endl;
}
Output:-
Note:- In the c part, if you want to check whether the new vegetable that you are adding to the vector is already present or not, then you can update your c part, with the following code:
if(find(vegs.begin(),vegs.end(),"Cauliflower") ==
vegs.end())
vegs.push_back("Cauliflower");
if(find(vegs.begin(),vegs.end(),"Capsicum") == vegs.end())
vegs.push_back("Capsicum");
However, the question strictly ask to write only two line of code, that's why, I didn't opt this method.