c++11 - nested unordered_maps in c++ -


i'm in process of re-writting program have in java c++. having lot of trouble complex data structure using:

unordered_map< string, unordered_map<string, list<string> > > 

it took me while able figure out how add 'items' (for lack of better word) unordered_map. however, come because cannot figure out how retrieve items put in using unordered_map::find.

my code below:

/* * queryddex.cpp * *  created on: aug 13, 2013 *      author: zach graceffa */  #include <zorba/store_manager.h> #include <zorba/xquery_exception.h> #include <zorba/zorba.h> #include <zorba/iterator.h> #include <zorba/xquery.h> #include <zorba/item.h>  #include <tr1/unordered_map> #include <string> #include <fstream> #include <list>  using namespace zorba; using namespace std; using namespace tr1;  void runquery (char * infile) throw(zorbaexception) { //create return variable unordered_map< string, unordered_map<string, list<string> > > nodecontainer;  //open file ifstream myfile; const char * ext = ".xq"; myfile.open(strcat(infile, ext), ifstream::in);  //instantiate zorba object void* lstore = zorba::storemanager::getstore(); zorba* lzorba = zorba::getinstance(lstore);  //feed file string string line; string xqdoc;  if (myfile.is_open()) {     while (myfile.good())     {       getline (myfile, line);       xqdoc += (line + "\n");     }     myfile.close(); } else     xqdoc = "err";  //compile query xquery_t lquery = lzorba->compilequery(xqdoc);  //create iterator , open can used iterator_t parentiterator = lquery->iterator(); parentiterator->open();  //create empty item future use item litem;  while (parentiterator->next(litem)) {     //create iterator iterate on child nodes belong parent     iterator_t childiterator = litem.getchildren();      //open iterator future use     childiterator->open();      //create empty item, used store child nodes.     item child;      //select first child node     while(childiterator->next(child)){         unordered_map<string, list<string> > childone;          iterator_t grandchilditerator = child.getchildren();         grandchilditerator->open();          item grandchild;          //create empty item hold section tag name.         item sectionname;         child.getnodename(sectionname);         nodecontainer.insert(pair<string, unordered_map<string, list<string> > >(sectionname.getstringvalue(), childone));          while(grandchilditerator->next(grandchild)){              list<string> grandchildren;              //create empty item hold contents of tag name             item tagname;              //put tag name in variable tagname             grandchild.getnodename(tagname);              unordered_map<string, list<string> > temp;              unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodecontainer.find(sectionname.getstringvalue());              if (temp.key_eq(tagname.getstringvalue())){                 list<string> s = temp.find(tagname.getstringvalue());                 s.insert(grandchild.getstringvalue());                 temp.put(sectionname.getstringvalue(), s);                 }else{                     grandchildren.add(grandchild.getstringvalue());                     temp.insert(tagname.getstringvalue(), grandchildren);                 }             nodecontainer.insert(pair<string, unordered_map<string, list<string> > >(sectionname.getstringvalue(), temp));              //release memory consumed tagname             tagname.close();             //free tagname;              }//grandchild-loop             //release memory consumed item grandchild             grandchild.close();             //delete grandchild;     }//child-loop }//end parent-loop } 

i give entire file working on. there lot of errors pasted java code directly c++ ide , working @ line line. please focus on line of code:

unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodecontainer.find(sectionname.getstringvalue()); 

another thing should add rusty @ c++ if there better way accomplish functionality

unordered_map< string, unordered_map<string, list<string> > > 

i ears.

thank reading far:)

to more specific error message break down problematic line into:

const string &keytotemp(sectionname.getstringvalue()); unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodecontainer.find(keytotemp); 

then once that's working next steps along these lines:

with minimum changes code suppose you're missing:

temp = got->second; 

find gives iterator element , value_type of map element pair<keytype, valuetype> hence use of second. though copy nested map.

maybe better use reference instead. in case line asking @ become:

 unordered_map<string, list<string> > &temp(nodecontainer.find(sectionname.getstringvalue())->second); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -