Write a program using the C++ STL map container to implement simple management of fruit prices, including operations such as adding, printing, searching, modifying, and deleting. In this program, you create a map called `fruitPrices` to store the prices of different fruits. Then add some initial fruit prices, print the current fruit prices, and then allow the user to search for, modify, or delete a fruit price. Finally, print the updated fruit prices.
时间: 2024-03-07 18:48:39 浏览: 47
The C++ STL
Sure, here's an example program that uses the C++ STL map container to implement simple management of fruit prices:
```c++
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// Create a map called fruitPrices to store the prices of different fruits
map<string, double> fruitPrices;
// Add some initial fruit prices
fruitPrices["apple"] = 1.99;
fruitPrices["banana"] = 0.99;
fruitPrices["orange"] = 2.49;
fruitPrices["grape"] = 3.99;
// Print the current fruit prices
cout << "Current fruit prices:" << endl;
for (auto it = fruitPrices.begin(); it != fruitPrices.end(); it++) {
cout << it->first << ": $" << it->second << endl;
}
// Allow the user to search for, modify, or delete a fruit price
string searchFruit;
cout << "Enter a fruit to search for, modify, or delete: ";
cin >> searchFruit;
// Search for the fruit
auto searchIter = fruitPrices.find(searchFruit);
if (searchIter == fruitPrices.end()) {
cout << "Fruit not found." << endl;
} else {
cout << "Found " << searchIter->first << ": $" << searchIter->second << endl;
// Modify the fruit price
char modifyChoice;
cout << "Do you want to modify the price? (Y/N): ";
cin >> modifyChoice;
if (modifyChoice == 'Y' || modifyChoice == 'y') {
double newPrice;
cout << "Enter the new price: $";
cin >> newPrice;
searchIter->second = newPrice;
cout << "Price updated." << endl;
}
// Delete the fruit price
char deleteChoice;
cout << "Do you want to delete the price? (Y/N): ";
cin >> deleteChoice;
if (deleteChoice == 'Y' || deleteChoice == 'y') {
fruitPrices.erase(searchIter);
cout << "Price deleted." << endl;
}
}
// Print the updated fruit prices
cout << "Updated fruit prices:" << endl;
for (auto it = fruitPrices.begin(); it != fruitPrices.end(); it++) {
cout << it->first << ": $" << it->second << endl;
}
return 0;
}
```
In this program, we first create a `map<string, double>` called `fruitPrices` to store the prices of different fruits. We then add some initial fruit prices, and print the current fruit prices using a for-loop that iterates over the `fruitPrices` map.
Next, we allow the user to search for, modify, or delete a fruit price. We prompt the user to enter a fruit to search for, and then use the `find` method of the `fruitPrices` map to search for the fruit. If the fruit is found, we prompt the user to modify or delete the fruit price, and then update the `fruitPrices` map accordingly using the `[]` or `erase` methods.
Finally, we print the updated fruit prices using another for-loop that iterates over the `fruitPrices` map.
阅读全文