Reputation: 69
I'm trying to make a python-like dictionary in C++, and using was suggested many times but in my case i want to label my lists with words instead of "1, 2, 3, 4...", is that possible?
Example dict: "Cookie": "sugar", "chocolate", "flour" "Pie": "milk", "apples", "flour"
etc etc
Upvotes: 1
Views: 420
Reputation: 725
You can do something really beautiful like this using the data structure proposed by @Retired Ninja. You can use unordered_map
instead of map
for better performance, since the order of the indexes doesn't matter:
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main()
{
unordered_map<string, vector<string>> dict = {
{"Cookies", {"sugar", "chocolate", "flour"}},
{"Pies", {"milk", "apples", "flour"}},
{"Drinks", {"Refrigerant", "Beer", "Juice"}},
//And so on... You can add values later
};
for(const auto& object : dict){
cout<<object.first<<": {";
for(const auto& value : object.second){
cout<<value<<", ";
}
cout<<object.first<<'}';
cout<<endl;
}
return 0;
}
Upvotes: 2