Multi level hash/dictionary creation in C++

别等时光非礼了梦想. 提交于 2019-12-12 01:40:01

问题


I am a bit lost. i have a csv file like the following

USN Name   DOB   Sem   Percentage
111 abc   07/03   3      88
112 cde   18/07   4      77
123 ghi   15/11   4      80

I want to create a dictionary kind of structure (multilevel dictionary- Dictionary <string, Dictionary<string, string>>) using C++. Here i want to store the 1st line as key i.e USN, Name, DOB ... as keys of the hash and the values in that columns as values of the hash. Is it possible? Any help will be greatly appreciated .. Thanks in advance.


回答1:


Given your question, you should be using unordered_map, which is standard in C++ 11 and very common in previous implementations. If performance is not too important (or the maps really small) you can also use map. Then you can do this:

using namespace std;
unordered_map<string, unordered_map<string, string> > dict;

After, it's a question of parsing your CSV file ...




回答2:


If you have a structure defined having these fields USN,Name, DOB, Sem, Percentage with datatypes as you choose appropriately, then USN (std::string) could be key and structure (Say MyStruct type) could be value. So hash_map having string as key and MyStruct as value typedef hash_map MyDictionary; should do



来源:https://stackoverflow.com/questions/23340565/multi-level-hash-dictionary-creation-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!