Question

In: Computer Science

Please use c++ Consider the code on the next page. It creates a vector of five...

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:

  1. Declare an iterator for a vector of strings, move it to the position of 'Tomato', and erase it because it's not a vegetable.
  2. With the same iterator, use .begin() to point it to the beginning of the list, and erase the first item, ‘Broccoli’ (Because no one likes broccoli.)
  3. Write two lines of code that will add two vegetable names that you DO like that isn't already on this list.

#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;

}

Solutions

Expert Solution

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.


Related Solutions

urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a...
Write a C# code that creates objects and classes with their member functions for KrisFlyer, a Singapore Airlines Loyalty program. You are asked to write an inheritance hierarchy discount system that benefits KrisFlyer members program to calculate their profit. A brief about KrisFlyer is that it is useful for those who fly on Singapore Airlines (its partners like Virgin Australia and Air New Zealand) frequently. KrisFlyer miles can be earned through credit cards, flying and bonus miles promotions. The miles...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a rectangular matrix A of dimensions 4X2 (4- rows, 2-columns); Create a matrix transpose to the above matrix A. Call that matrix AT; Calculate matrix products: A*AT and AT*A. Present the results. What are the dimensions of those two product matrices; Square matrixes sometimes have an inverse matrix. Try calculating inverse matrices (or matrixes, if you prefer) of above matrices (matrixes) A*AT and AT*A; Extend...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT