c++中嵌套的无序映射
nested unordered_maps in c++
我正在将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);
相关文章:
- 递归无序映射
- 正在将无序映射设置为无序映射的值
- 智能指针作为无序映射键,并通过引用进行比较
- 打印无序映射的第二个元素,即集合
- 由并发无序映射查找线程调用的函数是否安全?
- 将大数字(10-12 位数字)存储在无序映射中<字符串,整数>
- 使用无序映射在STL中存储键值对
- 为C++中的无序映射获取给定输入键的错误值
- 将大型对象存储在无序映射中是否效率低下
- 在无序映射的结构化绑定中推导类型
- 使用无序映射进行错误索引
- 将一个向量对放在一个无序映射与一个映射中
- 如何在c++中修改无序映射中的每个值
- C++:使用一对(cpp_int,int)整数作为无序映射中的键(其中cpp_int是boost多精度整数)
- 将 [] 索引到无序映射时出现段错误
- 以std::字符串作为关键字,按字典顺序对一个无序映射进行排序
- 无序映射存储桶的节点大小
- 在无序映射<字符串上使用 find(),向量<string>>与 C++ 中的有序映射花费相同的时间
- 以三个无符号字符为关键字的无序映射
- 无序映射比c++中的映射慢吗