[英]can I build a vector of vectors of structs with vectors of structs? (yes, really)
我正在尝试构建一个相对复杂的数据结构(对我而言)。 我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。 该表由结构向量的向量构成:(vector <vector> vecName;)。 我很幸运。 每个唯一的单词都被散列到向量中的索引位置。 向量的第二个维度(结构的向量)存储有关正在读取的文件以及在文件中找到该单词的信息。 对于我读取的每个文件,如果我多次找到某个单词,则在结构中递增计数,并且带有整数的结构向量存储该单词存储在文件中的所有位置的信息。
我有两件我想要帮助的项目:
这是cmpilation错误。 所有三个错误都引用结构中的结构向量:
'class std :: vector>'没有名为'theLoc'的成员
'class std :: vector>'没有名为'theStart'的成员
'class std :: vector>'没有名为'theEnd'的成员
如果我按照EboMike的建议调整代码,原始错误就会消失,但我会得到:
我得到一个不同的错误,我不能发布因为编辑认为我发布了超链接。 摘要是:*'testProps.wordProps :: theWordLoc:theLoc中的成员请求'push_back',它是非类型的'int'*
这是我的代码和图表链接(来自我的博客)我看到的数据结构:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std;
struct wordLoc
{
int theLoc; // the location of the word in theFile
int theStart; // the beginning of the sentence
int theEnd; // the end of the sentence
};
struct wordProps // stores word info to be placed in array
{
string theFile; // stores the file where theWord is found
int theCount; // increments with each occurence of theWord
vector <wordLoc> theWordLoc; // stores the wordLoc info for each occurence of theWord
};
int main()
{
int Tsize = 20000;
wordProps testProps;
testProps.theFile = "test1";
testProps.theCount = 1;
testProps.theWordLoc.theLoc.push_back(200);
testProps.theWordLoc.theStart.push_back(1);
testProps.theWordLoc.theEnd.push_back(15);
vector < vector <wordProps> > theWordProps;
theWordProps.resize(Tsize);
theWordProps[0].push_back(testProps);
cout << "index[0] = " << theWordProps[0].front().theFile << endl;
cout << "index[0] = " << theWordProps[0].front().theCount << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theLoc << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theStart << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theEnd << endl;
cout << "size of theWordProps[0] = " << theWordProps[0].size();
cout << endl;
}
首先是编译错误:你可能是指这一行:
testProps.theWordLoc.theLoc.push_back(200);
testProps.theWordLoc.theStart.push_back(1);
testProps.theWordLoc.theEnd.push_back(15);
theWordLoc是一个向量,所以你需要对它进行处理,例如:
testProps.theWordLoc[0].theLoc = 200;
或者,如果还没有:
wordLoc wordLocData;
worldLocData.theLoc = 200;
worldLocData.theStart = 1;
worldLocData.theEnd = 15;
testProps.theWorldLoc.push_back(worldLocData);
至于你的实际问题:这是一个可行的解决方案吗? 是的。 但是,您期望得到多少数据? 它的持久性如何? 如果答案是“吨,长”,我会选择数据库。 有一个用于worldLoc的表,一个用于wordProps,一个用于更高级别的向量,并且事情更快更清洁。
另外,我不喜欢顶级矢量。 我不明白你打算在那里做的结构(我只是看了一眼图),但听起来你正在寻找一个hashmap。
我不知道数据结构的设计选择,除了使它成为一个hasmap,但你的代码几乎是正确的!
看看我的评论:
int main()
{
int Tsize = 20000;
wordProps testProps;
testProps.theFile = "test1";
testProps.theCount = 1;
// create your wordLoc object
wordLoc wl;
wl.theLoc = 200;
wl.theStart = 1;
wl.theEnd = 15;
// put it into the vector
testProps.theWordLoc.push_back(wl);
vector < vector <wordProps> > theWordProps;
theWordProps.resize(Tsize);
theWordProps[0].push_back(testProps);
cout << "index[0] = " << theWordProps[0].front().theFile << endl;
cout << "index[0] = " << theWordProps[0].front().theCount << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theLoc << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theStart << endl;
cout << "index[0] = " << theWordProps[0].front().theWordLoc[0].theEnd << endl;
cout << "size of theWordProps[0] = " << theWordProps[0].size();
cout << endl;
}
在testProps.theWordLoc.theLoc
您指的是向量theWordLoc
的theLoc
成员。 这简直是不可接受的。 你应该使用像testProps.theWordLoc[0].theLoc
这样的东西。
也许对于数据结构,多图可能是你的朋友在这里取代向量的顶级lvl向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.