c++; 我正在编写java中C++程序。我使用的复杂数据结构有很多问题: unordered_map< string, unordered_map<string, list<string> > > unordered\u-map
这花了我一段时间,但我终于想出了如何在无序的地图上添加“项目”(因为没有更好的词)。但是,我来找您是因为我无法找到如何使用无序的_map::find检索我放入其中的项目 我的代码如下:c++; 我正在编写java中C++程序。我使用的复杂数据结构有很多问题: unordered_map< string, unordered_map<string, list<string> > > unordered\u-map,c++,c++11,hashtable,unordered-map,zorba,C++,C++11,Hashtable,Unordered Map,Zorba,这花了我一段时间,但我终于想出了如何在无序的地图上添加“项目”(因为没有更好的词)。但是,我来找您是因为我无法找到如何使用无序的_map::find检索我放入其中的项目 我的代码如下: /* * QueryDDex.cpp * * Created on: Aug 13, 2013 * Author: Zach Graceffa */ #include <zorba/store_manager.h> #include <zorba/xquery_exception.
/*
* 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
}
/*
*querydex.cpp
*
*创建日期:2013年8月13日
*作者:Zach Graceffa
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间zorba;
使用名称空间std;
使用名称空间tr1;
void runQuery(char*infle)抛出(ZorbaException)
{
//创建返回变量
无序映射<字符串,无序映射>节点容器;
//打开文件
ifstreammyfile;
常量字符*ext=“.xq”;
open(strcat(infle,ext),ifstream::in);
//实例化Zorba对象
void*lStore=zorba::StoreManager::getStore();
Zorba*lZorba=Zorba::getInstance(lStore);
//将文件馈送到字符串中
弦线;
字符串xqDoc;
如果(myFile.is_open())
{
while(myFile.good())
{
getline(myFile,line);
xqDoc+=(第+“\n”行);
}
myFile.close();
}
其他的
xqDoc=“err”;
//编译查询
XQuery\u t lQuery=lZorba->compileQuery(xqDoc);
//创建一个迭代器并打开它,以便可以使用它
迭代器\u t parentIterator=lQuery->Iterator();
父迭代器->打开();
//创建一个空项以供将来使用
项目文字;
while(parentIterator->next(lItem))
{
//创建一个迭代器来迭代属于父节点的所有子节点
迭代器\u t childIterator=lItem.getChildren();
//打开迭代器以备将来使用
子迭代器->打开();
//创建一个空项,用于存储子节点。
项目子项;
//选择第一个子节点
while(儿童迭代器->下一个(儿童)){
无序地图儿童一号;
迭代器Iterator=child.getChildren();
迭代器->打开();
项目孙子;
//创建一个空项以保存节标记名称。
项目名称;
child.getNodeName(节名);
insert(成对(sectionName.getStringValue(),childOne));
while(孙子迭代器->下一步(孙子)){
列出孙辈名单;
//创建一个空项以保存标记名的内容
项目标记名;
//将标记名放入变量标记名中
grant.getNodeName(标记名);
无序映射温度;
无序映射::const\u iterator get=nodeContainer.find(sectionName.getStringValue());
if(临时键(标记名.getStringValue())){
列表s=temp.find(标记名.getStringValue());
s、 插入(grant.getStringValue());
临时输入(sectionName.getStringValue(),s);
}否则{
add(grander.getStringValue());
temp.insert(标记名.getStringValue(),孙辈);
}
insert(成对(sectionName.getStringValue(),temp));
//释放标记名消耗的所有内存
标记名.close();
//自由标记名;
}//孙子环
//释放项目孙子所消耗的所有内存
孙子。close();
//删除孙辈;
}//子循环
}//结束父循环
}
我给你我目前正在处理的整个文件。当我把java代码直接粘贴到C++ IDE中时,会有很多错误,我只是一行一行地工作。请关注这一行代码:
unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodeContainer.find(sectionName.getStringValue());
unordered_-map::const_迭代器get=nodeContainer.find(sectionName.getStringValue());
另一件我应该补充的是,我在C++中生锈,所以如果有更好的方法来完成这个功能比
unordered_map< string, unordered_map<string, list<string> > >
unordered\u-map
我洗耳恭听
感谢您阅读本文:)要获得更具体的错误消息,您可以将有问题的行分解为:
const string &keyToTemp(sectionName.getStringValue());
unordered_map< string, unordered_map<string, list<string> > >::const_iterator got = nodeContainer.find(keyToTemp);
find
为元素提供了一个迭代器,map元素的值类型是对
,因此使用了second。
但它将复制嵌套贴图
也许用一个引用来代替它会更好。在这种情况下,您要求我们查看的行将变成:
unordered_map<string, list<string> > &temp(nodeContainer.find(sectionName.getStringValue())->second);
unordered_map&temp(nodeContainer.find(sectionName.getStringValue())->second);
“我在使用复杂的数据结构时遇到了很多麻烦”——这是一个很好的确认。如果作为作者,它对你来说很复杂,那么对这里的人们来说,它可能会更复杂。通过将问题分解为更小的可管理子问题,您有更好的机会解决问题,无论是您自己还是其他人。您正在将字符串映射到字符串映射到字符串列表。。。。复杂的部分是什么?复制起来可能很贵,但不太复杂。@Arun谢谢,我以后会记住的。@WhozCraig我猜复杂的意思是语法很复杂。我在Java中使用它没有问题。@zg303:我发现在嵌套容器时,当您键入def内部容器,或者将其设置为实际类型时,一切都会变得非常简单<代码>typedef无序映射innermap代码>然后<代码>无序映射节点容器代码>。最后:无序映射::常量迭代器get=nodeContainer.find…`实际上,我的错误是find函数。我得到一个错误,即我的参数无效,即使它是一个与键类型相同的字符串。我真的不知道是什么。我将把这个概念带到这个程序之外,并做一个例子来看看这里发生了什么。@zg303啊哈,好的,我已经调整了我的答案,添加了一个建议,mi
unordered_map<string, list<string> > &temp(nodeContainer.find(sectionName.getStringValue())->second);