0

我正在将我用 Java 编写的程序重写为 C++。我在使用复杂的数据结构时遇到了很多麻烦:

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

我花了一段时间,但我最终能够弄清楚如何将“项目”(因为没有更好的词)添加到 unordered_map。但是,我来找你是因为我不知道如何使用 unordered_map::find 检索我放入其中的项目。

我的代码如下:

/*
* 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
}

我给你我目前正在处理的整个文件。当我将 java 代码直接粘贴到我的 c++ ide 中并且只是逐行处理时,出现了很多错误。请关注这行代码:

unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodeContainer.find(sectionName.getStringValue());

我应该补充的另一件事是我对 c++ 生疏了,所以如果有比

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

洗耳恭听。

谢谢你读到这里:)

4

1 回答 1

2

要获得更具体的错误消息,您可以将有问题的行分解为:

const string &keyToTemp(sectionName.getStringValue());
unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodeContainer.find(keyToTemp);

然后,一旦它起作用,接下来的步骤可能会沿着这些方向:

通过对您的代码进行最少的更改,我想这就是您所缺少的:

temp = got->second;

find为您提供元素的迭代器,并且 map 元素的 value_type 是 apair<KeyType, ValueType>因此使用 second. 这虽然会复制嵌套地图。

也许使用对它的引用会更好。在这种情况下,您要求我们查看的行将变为:

 unordered_map<string, list<string> > &temp(nodeContainer.find(sectionName.getStringValue())->second);
于 2013-08-16T00:40:37.570 回答