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-19 10:02:33 浏览: 79
Here is a possible implementation of the program:
```c++
#include <iostream>
#include <vector>
#include <algorithm> // for std::replace
void print_vector(const std::vector<std::string>& v) {
std::cout << "[";
for (size_t i = 0; i < v.size(); ++i) {
std::cout << v[i];
if (i < v.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
int main() {
std::vector<std::string> shopping_list;
std::cout << "Empty list: ";
print_vector(shopping_list);
shopping_list.push_back("eggs");
shopping_list.push_back("milk");
shopping_list.push_back("sugar");
shopping_list.push_back("chocolate");
shopping_list.push_back("flour");
std::cout << "Initial list: ";
print_vector(shopping_list);
shopping_list.pop_back();
std::cout << "List after removing last item: ";
print_vector(shopping_list);
shopping_list.push_back("coffee");
std::cout << "List after adding coffee: ";
print_vector(shopping_list);
for (auto& item : shopping_list) {
if (item == "sugar") {
item = "honey";
}
}
std::cout << "List after replacing sugar with honey: ";
print_vector(shopping_list);
shopping_list.erase(std::remove(shopping_list.begin(), shopping_list.end(), "milk"), shopping_list.end());
std::cout << "List after removing milk: ";
print_vector(shopping_list);
auto it = std::find(shopping_list.begin(), shopping_list.end(), "chocolate");
if (it != shopping_list.end()) {
shopping_list.insert(it, "tea");
}
std::cout << "List after inserting tea before chocolate: ";
print_vector(shopping_list);
return 0;
}
```
The `print_vector` function takes a reference to a const vector of strings and prints its contents between square brackets, separated by commas and spaces, followed by a newline.
The `main` function creates an empty vector of strings called `shopping_list`, prints it using `print_vector`, appends the items using `push_back`, prints the list again, removes the last item using `pop_back`, prints the list again, appends "coffee", prints the list again, replaces "sugar" with "honey" using a range-based for loop and `std::replace`, prints the list again, removes "milk" using `std::find` and `std::erase`, prints the list again, inserts "tea" before "chocolate" using `std::find` and `std::insert`, and prints the list one last time.
The output of the program should be:
```
Empty list: []
Initial list: [eggs, milk, sugar, chocolate, flour]
List after removing last item: [eggs, milk, sugar, chocolate]
List after adding coffee: [eggs, milk, sugar, chocolate, coffee]
List after replacing sugar with honey: [eggs, milk, honey, chocolate, coffee]
List after removing milk: [eggs, honey, chocolate, coffee]
List after inserting tea before chocolate: [eggs, honey, tea, chocolate, coffee]
```
阅读全文