简体   繁体   中英

C++:Storing Items in a vector into a map

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.

You could fill the map of stores in the first while loop. The following program demonstrates how to fill the map and how to iterate over the items in the map.

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

Nevertheless, the code would need further cleanup to be used for real programs. Whether to retrieve the store as return value of CreateList() is discutable.

Try to simplify the answer: CreateList() should return a pair:

pair<string, vector<string>> 

which is store_name(string), item_list(vector).

So that in main function, you can insert it to map > list:

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

Your other code looks good for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM