我正在尝试构建一个相对复杂的数据结构(对我来说)。我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。该表是由一个矢量的矢量的结构体构成的:(vector < vector > vecName;)。这个我已经做到了。每个唯一的单词都被散列成向量中的索引位置。向量的第二维(结构体的向量)存储有关正在读取的文件和单词在文件中的位置的信息。对于我读取的每个文件,如果我发现某个单词多次出现,则在结构体中递增计数,并且带有整数的结构体向量存储单词存储在文件中的所有位置的信息。
我有两个需要协助的项目:
1. 我想知道是否有人有比我的建议更好的数据结构实现建议。包含一些独立数据成员的类是否可能更可用? 2. 看起来我要么有语法错误导致编译错误,要么我只是试图构建一个向量类不支持的结构。
以下是编译错误。所有三个错误都涉及结构体内部的结构体向量:
我有两个需要协助的项目:
1. 我想知道是否有人有比我的建议更好的数据结构实现建议。包含一些独立数据成员的类是否可能更可用? 2. 看起来我要么有语法错误导致编译错误,要么我只是试图构建一个向量类不支持的结构。
以下是编译错误。所有三个错误都涉及结构体内部的结构体向量:
'class std::vector >'没有名为'theLoc'的成员
'class std::vector >'没有名为'theStart'的成员
'class std::vector >'没有名为'theEnd'的成员
如果我像EboMike建议的那样调整代码,原始错误就消失了,但是我接着会得到:
我得到了一个不同的错误,无法发布,因为编辑器认为我在发布超链接。总结是:*'testProps.wordProps :: theWordLoc:theLoc'中请求非类类型'int'的'member push_back'*
这是我的代码和一个数据结构图的链接(来自我的博客):
#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;
}