c++中嵌套的无序映射

nested unordered_maps in c++

本文关键字:无序 映射 嵌套 c++      更新时间:2023-10-16

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

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

这花了我一段时间,但我最终找到了如何将"items"(因为没有更好的词)添加到无序映射中。然而,我来找你是因为我不知道如何使用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> > >

我洗耳恭听。

感谢您阅读本文:)

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

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是pair<KeyType, ValueType>,因此使用了second。不过,这将复制嵌套的映射。

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

 unordered_map<string, list<string> > &temp(nodeContainer.find(sectionName.getStringValue())->second);