简体   繁体   中英

Multi level hash/dictionary creation in C++

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 ie 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.

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

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 ...

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