Imagine you are writing a program to manage a shopping list. Using a vector to hold the shopping list items, write a print function to print out the contents of a vector of strings. Test your print function with a main program that does the following: Create an empty vector. Print it. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print it. Remove the last element from the vector. Print it. Append the item, "coffee" to the vector. Print it. Write a loop that searches for the item, "sugar" and replace it with "honey." Print the vector. Write a loop that searches for the item, "milk," and then remove it from the vector. Print the vector. Search for the item, "chocolate," and insert the item "tea" before it. Print the vector one more time.
时间: 2024-01-25 07:05:04 浏览: 65
Sure, here's an example program that does what you're asking for:
```cpp
#include <iostream>
#include <vector>
#include <string>
void printList(const std::vector<std::string>& list) {
for (const auto& item : list) {
std::cout << item << std::endl;
}
}
int main() {
std::vector<std::string> shoppingList;
// Print the empty list
std::cout << "Empty list:" << std::endl;
printList(shoppingList);
// Add items to the list
shoppingList.push_back("eggs");
shoppingList.push_back("milk");
shoppingList.push_back("sugar");
shoppingList.push_back("chocolate");
shoppingList.push_back("flour");
// Print the list with items
std::cout << std::endl << "List with items:" << std::endl;
printList(shoppingList);
// Remove the last item from the list
shoppingList.pop_back();
// Print the updated list
std::cout << std::endl << "List with last item removed:" << std::endl;
printList(shoppingList);
// Add "coffee" to the list
shoppingList.push_back("coffee");
// Print the updated list
std::cout << std::endl << "List with coffee added:" << std::endl;
printList(shoppingList);
// Replace "sugar" with "honey"
for (auto& item : shoppingList) {
if (item == "sugar") {
item = "honey";
}
}
// Print the updated list
std::cout << std::endl << "List with sugar replaced with honey:" << std::endl;
printList(shoppingList);
// Remove "milk" from the list
for (auto it = shoppingList.begin(); it != shoppingList.end(); ) {
if (*it == "milk") {
it = shoppingList.erase(it);
} else {
++it;
}
}
// Print the updated list
std::cout << std::endl << "List with milk removed:" << std::endl;
printList(shoppingList);
// Insert "tea" before "chocolate"
for (auto it = shoppingList.begin(); it != shoppingList.end(); ++it) {
if (*it == "chocolate") {
shoppingList.insert(it, "tea");
break;
}
}
// Print the final list
std::cout << std::endl << "Final list with tea added:" << std::endl;
printList(shoppingList);
return 0;
}
```
The output of the program should look like this:
```
Empty list:
List with items:
eggs
milk
sugar
chocolate
flour
List with last item removed:
eggs
milk
sugar
chocolate
List with coffee added:
eggs
milk
sugar
chocolate
coffee
List with sugar replaced with honey:
eggs
milk
honey
chocolate
coffee
List with milk removed:
eggs
honey
chocolate
coffee
Final list with tea added:
eggs
honey
tea
chocolate
coffee
```
阅读全文