I'm in the process of re-writting a program I have in Java to C++. I am having a lot of trouble with the complex data structure I am using:
unordered_map< string, unordered_map<string, list<string> > >
It took me a while but I was eventually able to figure out how to add 'items' (for lack of a better word) to the unordered_map. However, I come to you because I cannot figure out how to retrieve the items that I put in it using unordered_map::find.
My code is 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 the Zorba Object
void* lStore = zorba::StoreManager::getStore();
Zorba* lZorba = Zorba::getInstance(lStore);
//Feed file into string
string line;
string xqDoc;
if (myFile.is_open())
{
while (myFile.good())
{
getline (myFile, line);
xqDoc += (line + "\n");
}
myFile.close();
}
else
xqDoc = "err";
//Compile the Query
XQuery_t lQuery = lZorba->compileQuery(xqDoc);
//Create an Iterator and open it so it can be used
Iterator_t parentIterator = lQuery->iterator();
parentIterator->open();
//Create an empty Item for future use
Item lItem;
while (parentIterator->next(lItem))
{
//Create an iterator to iterate over all the child nodes that belong to the parent
Iterator_t childIterator = lItem.getChildren();
//Open the iterator for future use
childIterator->open();
//Create an empty item, which will be used to store the child nodes.
Item child;
//Select the first child node
while(childIterator->next(child)){
unordered_map<string, list<string> > childOne;
Iterator_t grandChildIterator = child.getChildren();
grandChildIterator->open();
Item grandChild;
//Create an empty item to hold the 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 an empty Item to hold the contents of tag name
Item tagName;
//Put the 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 any memory consumed by tagName
tagName.close();
//free tagName;
}//grandchild-loop
//Release any memory consumed by Item grandChild
grandChild.close();
//delete grandChild;
}//child-loop
}//end parent-loop
}
I give you the entire file that I am currently working on. There are a lot of errors as I pasted the java code directly into my c++ ide and am simply working at it line by line. Please focus on this line of code:
unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodeContainer.find(sectionName.getStringValue());
Another thing I should add is that I am rusty at c++ so if there is a better way to accomplish this functionality than a
unordered_map< string, unordered_map<string, list<string> > >
I am all ears.
Thank you for reading this far:)
To get a more specific error message you could break down your 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 the next steps could be along these lines:
With the minimum changes to your code I suppose this is what you're missing:
temp = got->second;
find
gives you an iterator to the element and the value_type of a map element is a pair<KeyType, ValueType>
hence the use of second.
That though will copy the nested map.
Maybe it would be better to use a reference to it instead. In which case the line you asking us to look at would become:
unordered_map<string, list<string> > &temp(nodeContainer.find(sectionName.getStringValue())->second);