0

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.

SauShi
  • 3
  • 1
  • 4
  • 6
    what should be used as the key, and what as the value? Check out [unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) – Dmitry Ledentsov Apr 28 '14 at 12:02
  • http://en.cppreference.com/w/cpp/container/map – Adam Burry Apr 28 '14 at 12:03
  • If you need multiple indices, consider `boost::container::multimap`. – Deduplicator Apr 28 '14 at 12:23
  • 1
    Please be more precise with what you mean as "a dictionary kind of structure". And the "like C#" won't help. – PierreBdR Apr 28 '14 at 12:26
  • @DmitryLedentsov: I have edited my question for you.. – SauShi Apr 28 '14 at 12:39
  • @PierreBdR: dictionary as in multilevel dictionary Dictionary > where i can store the 1st line as key and column value as values of the hash. As specified in the question. – SauShi Apr 28 '14 at 12:40
  • @Deduplicator: I think you confuse multimap with boost::multi_index_container. I'm not sure yet if either is suitable for this use case, but anyway "multiple indices" are implemented by MIC not multimap. – John Zwinck Apr 28 '14 at 12:42
  • @SauShi: Are you sure you want to nest the lookups like that? It will not be efficient. Perhaps it would be better to just store all the rows in a list and then store iterators into the list in separate maps, one for each column/key. Otherwise, if you choose to nest the indexing, you will not be able to efficiently look up by anything not using the first key. – John Zwinck Apr 28 '14 at 12:45

2 Answers2

0

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

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

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

PierreBdR
  • 42,120
  • 10
  • 46
  • 62