问题
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