2

I'm trying to mimic the python dictionary in C++. For example I want to build something like

{"The Dark Night Rises": {"year": 2012, "StoryLine": "this is the story.....", "Genres": ["action","crime","Thriller"]}}

I'm using the STL map and lists for building up this kind of dictionary. But I'm not sure how to use the iterators. Can some one help me with an example of building the above dictionary. I started something like below to just build a simple dictionary like {"cast":["action","crime","Thriller"]}. I'm confused how to build the above mentioned multilevel dictionaries, and specially iterating over them.

#include<iostream>
#include<string>
#include<map>
#include<iterator>
#include<list>

using namespace std;

class MultiLevDict
{
private:
    list<string> lis;
    map<string,list<string> > MultiDict;
public:
    void Setter();
    void Display() const;
};

void MultiLevDict::Setter()
{
    string field;
    string cast;
    int sizeCast;
    cout<<"enter the field of the movie:";
    cin>>field;
    cout<<endl;
    cout<<"how many cast are there in this movie?:";
    cin>>sizeCast;
    for (int i=0; i<sizeCast; i++)
    {
        cin>>cast;
        lis.push_back(cast);
    }
    MultiDict[field]=lis;

}

void MultiLevDict::Display() const
{
    list<string>::iterator lisIt;

}
David Makogon
  • 69,407
  • 21
  • 141
  • 189
Rkz
  • 1,237
  • 5
  • 16
  • 30

2 Answers2

2

Looping over a container is as easy as iterating from begin() to end(). The iterator type is provided by the type of the container by adding ::iterator or ::const_iterator.

Here's a complete example. I tried to stick to your code. Note the comment about the new syntax in C++11.

#include<iostream>
#include<string>
#include<map>
#include<iterator>
#include<list>

using namespace std;

class MultiLevDict
{
private:
    list<string> lis;
    map<string,list<string> > MultiDict;
public:
    void Setter();
    void Display() const;
};

void MultiLevDict::Setter()
{
    string field;
    string cast;
    field="Abcd ";
    lis.push_back("Tom");
    lis.push_back("Eve");
    MultiDict[field]=lis;
// This works in C++11 :
    MultiDict["Efgh "]={"Joe","Lisa"};

}

void MultiLevDict::Display() const
{
   for(map<string,list<string> >::const_iterator it=MultiDict.begin();
       it!=MultiDict.end();++it){
      std::cout << "key: was: "<<it->first<<std::endl;
      for (list<string>::const_iterator it2=it->second.begin();
       it2!=it->second.end();++it2){
     std::cout << "   "<<it->first<< " contains " <<*it2<<std::endl;
      }
   }   
}

int main() {
   MultiLevDict myd;
   myd.Setter();
   myd.Display();   
}

the result is

key: was: Abcd 
  Abcd  contains Tom
  Abcd  contains Eve
key: was: Efgh 
  Efgh  contains Joe
  Efgh  contains Lisa
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • any special reason for preferring the `map` of `list`s to a `multimap`? – ev-br Jan 23 '12 at 23:17
  • @Zhenya. Nothing that I cared about. – Johan Lundberg Jan 23 '12 at 23:28
  • @JohanLundberg. I would like to extend my question by asking how can you store multiple datatypes inside a map? for example now we declare map> in this map we can store a key of string and a value of list type, now say I want to have {"someval":["val1"], "somval2":"a string here", "someval3":24}. I found a a link [CODE][http://www.dreamincode.net/forums/topic/129708-c-map-with-multiple-data-types/]. After seeing that link I am thinking something like map>. – Rkz Jan 31 '12 at 20:10
  • There are ways to do that, but are you sure you need that complication? If yes, read about boost.any, try to make it work and/else/then post a new question. Also search stackoverflow. – Johan Lundberg Jan 31 '12 at 20:26
0
void MultiLevDict::Display() const
{
    for (auto x : MultiDict)
        for (auto y : x.second)
            cout << x.first << ": " << y << endl;
}
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319