0

The main program must store the grocery lists in a map of key-value pairs called: lists. The key must be a string containing a grocery store name and the value must be a vector of strings containing the grocery list of items for that store.

Function i'm using

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);
    }

}

My problem is I can't figure out how to properly iterate through the vector created in CreateList() and add items in that vector to the map, which I'm also having a hard time figuring out how to get the grocery store name for. Would I have to have the CreateList() function return something in order to do this? This is my first attempt at a C++ program transitioning from python so please simplify all answers. Thanks for your time.

4

2 回答 2

1

您可以在第一个 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 上发布编译最小示例。如果编译器给出错误,则发布带有相应错误消息的最小示例。

于 2013-11-01T18:36:34.287 回答
0

尝试简化答案: CreateList() 应该返回一对:

pair<string, vector<string>> 

这是 store_name(string), item_list(vector)。

这样在 main 函数中,您可以将其插入到 map > list 中:

 list.insert(pair<string, vector<string>>);

您的其他代码对我来说看起来不错。

于 2015-06-13T06:18:06.573 回答