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...
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 write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
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...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT