【发布时间】:2010-12-04 06:43:35
【问题描述】:
我正在尝试构建一个相对复杂的数据结构(对我而言)。我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。该表由结构向量的向量构成:(vector vecName;)。我很幸运。每个唯一的词都被散列到向量中的一个索引位置。向量的第二个维度(结构向量)存储有关正在读取的文件以及在文件中找到单词的位置的信息。对于我读取的每个文件,如果我多次找到某个单词,则在结构中增加一个计数,并且带有整数的结构向量存储该单词存储在文件中的所有位置的信息。
我有两个项目需要帮助:
- 我很好奇是否有人提出了比我的建议更好的数据结构实现的建议。包含一些独立数据成员而不是这个庞然大物的类可能更有用吗?
- 看来我的语法错误导致编译错误,或者我只是尝试构建向量类不支持的结构。
这里是编译错误。所有三个错误都指结构内的结构向量:
“类 std::vector >”没有名为“theLoc”的成员
“类 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;
}
【问题讨论】:
-
“看来我的语法错误导致编译错误”...请发布错误消息的全文。
标签: c++ xcode data-structures vector struct