[英]C++:Storing Items in a vector into a map
主程序必須將雜貨店列表存儲在稱為“列表”的鍵值對映射中。 鍵必須是包含雜貨店名稱的字符串,並且值必須是包含該商店的雜貨項目列表的字符串向量。
我正在使用的功能
void CreateList()
{
std::string store;
std::string item;
std::cout << "What is the grocery store name" << std::endl;
std::cin >> store;
std::vector<std::string> store_list;
do {
std::cout << "Enter a list of items for this grocery store one at a time. When you are done, type done." << std::endl;
std::cin >> item;
if (item == "done") break;
store_list.emplace_back(item);
std::cout << "Added " << item << "to " << store << "list" << std::endl;
} while (true);
main(){
while (true)
{
int choice;
DisplayMenu();
cout<<"What menu choice would you like?"<<endl;
cin>>choice;
if (choice == 1)
CreateList();
if (choice == 2)
ExportLists();
if (choice == 3)
cout<<"Thank you for using!"<<endl;
break;
else
cout<<"Please enter a valid choice."<<endl;
}
map<string, vector<string> > list;
map<string, vector<string> >::iterator ListItr;
vector<string>::iterator VecItr;
for (ListItr = list.begin(); ListItr != list.end(); ++ListItr)
{
for (VecItr = ListItr ->second.begin();
VecItr != ListItr ->second.end();
++VecItr)
{
list[store].push_back(*VecItr);
}
}
我的問題是我不知道如何正確遍歷CreateList()中創建的向量並將該向量中的項目添加到地圖,這也讓我很難確定如何獲得食品雜貨店名稱。 為了執行此操作,是否需要CreateList()函數返回某些內容? 這是我第一次嘗試從python轉換C ++程序,因此請簡化所有答案。 謝謝你的時間。
您可以在第一個while循環中填寫商店地圖。 以下程序演示了如何填充地圖以及如何遍歷地圖中的項目。
#include <iostream>
#include <vector>
#include <string>
#include <map>
typedef std::vector<std::string> items_t;
typedef std::pair<std::string, items_t> store_t;
typedef std::map<std::string, items_t> stores_t;
store_t CreateList()
{
std::string store;
std::string item;
std::cout << "What is the grocery store name" << std::endl;
std::cin >> store;
std::vector<std::string> store_list;
while((
std::cout << "Enter a list of items for this grocery store one at a time."
<< "When you are done, type done." << std::endl,
std::cin >> item,
item != "done"))
{
store_list.emplace_back(item);
std::cout << "Added " << item << " to " << store << " list" << std::endl;
}
return store_t(store,store_list);
}
void DisplayMenu() {
std::cout << "DisplayMenu()\n";
}
int main() {
stores_t stores;
int choice;
do {
DisplayMenu();
std::cout<<"What menu choice would you like?\n";
std::cin>>choice;
switch (choice) {
case 1:
{
stores.emplace(CreateList());
}
break;
case 2:
// not implemented
case 3:
std::cout<<"Thank you for using!\n";
break;
default:
std::cout<<"Please enter a valid choice.\n";
}
} while (choice != 3);
// Look what stores we have:
for(auto iter=stores.begin(); iter != stores.end(); iter++) {
std::cout << "\n\nStore " << iter->first << " has the following items:\n";
items_t& items(iter->second);
for(auto iterItem=items.begin(); iterItem != items.end(); iterItem++) {
std::cout << *iterItem << ", ";
}
}
return 0;
}
但是,該代碼將需要進一步清理以用於實際程序。 是否可以檢索存儲作為CreateList()
返回值是有爭議的。
注意,您應該始終在stackoverflow上發布編譯最少的示例。 如果編譯器給出錯誤,則將最小示例與相應的錯誤消息一起發布。
嘗試簡化答案:CreateList()應該返回一對:
pair<string, vector<string>>
這是store_name(string),item_list(vector)。
這樣就可以在main函數中將其插入map> list:
list.insert(pair<string, vector<string>>);
您的其他代碼對我來說看起來不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.