2010-12-04 122 views
3

我试图建立一个相对复杂的数据结构(对我来说)。我的目标是从文本文档中读取单词,并将单词和一些特定属性编入一个散列表。该表由结构向量构成:(向量<向量> vecName;)。我很幸运。每个唯一字被散列到向量中的索引位置。矢量的第二维(结构向量)存储有关正在读取的文件的信息以及文件中找到的单词的位置。对于我读取的每个文件,如果我多次查找某个单词,则会在结构中增加一个计数,并且带有整数的结构向量将存储单词存储在文件中的所有位置的信息。我可以用结构向量构建一个结构向量向量吗? (是的,真的)

我有两个项目,我想与援助:

  1. 我很好奇,如果任何人有更好的数据结构实现比我建议的建议。包含一些独立数据成员而不是这个庞然大物的类可能更有用吗?
  2. 看来我的语法错误是导致编译错误,或者我试图构建一个向量类不支持的结构。

这里是cmpilation错误。所有这三个错误是指结构的载体结构内:

“类的std ::矢量>”没有名为“theLoc”
“类的std ::矢量>”没有名为“theStart”成员成员
“类的std ::矢量>”没有名为成员“附带一份最终”

如果我调整代码为EboMike表明,原来的错误消失,但后来我得到:

我得到一个不同的错误我不能发布,因为编辑认为我发布超链接。总结如下:*'请求'testProps.wordProps :: theWordLoc:theLoc中的成员'push_back',它是非类类型'int'*

这里是我的代码和一个图的链接

http://iamkevinfrye.com/blog/wp-content/uploads/2010/10/MicroSearch-Hash-Table-Data-Structure-Diagram.png

#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; 
} 
+2

“看来,我要么就导致一个编译错误语法错误” ......请张贴错误消息的全文。 – 2010-12-04 06:48:49

回答

1

我不知道数据结构的设计选择,除了使它成为一个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; 
} 
+0

谢谢Vic!这非常合理,我在将TestProps传递给WordProps时使用了相同类型的逻辑,但它在那里有点雾,并且我没有看到将wl传递给wordProps的相同模式。感谢您帮助这个小小的受害者! :)现在我可以休息一下了! – fryeguy 2010-12-04 07:32:41

3

编译错误第一:你可能指的是这一行:我的我怎么看的数据结构博客)

testProps.theWordLoc.theLoc.push_back(200); 
testProps.theWordLoc.theStart.push_back(1); 
testProps.theWordLoc.theEnd.push_back(15); 

theWordLoc是AV埃克特,所以你需要把它当作这样的,例如:

testProps.theWordLoc[0].theLoc = 200; 

或者,如果有什么都没有尚未:

wordLoc wordLocData; 
worldLocData.theLoc = 200; 
worldLocData.theStart = 1; 
worldLocData.theEnd = 15; 
testProps.theWorldLoc.push_back(worldLocData); 

至于你的实际问题:是一个可行的解决方案?是的。但是,您希望得到多少数据?它有多持久?如果答案是“吨,长”,我会去一个数据库,而不是。有一个worldLoc的表格,一个用于wordProps,一个用于更高级别的矢量,而且事情要快得多,干净得多。

此外,我不喜欢顶级向量。我不明白你打算在那里做的结构(我只是看了一下图表),但是这听起来像你在寻找一个hashmap。

+0

我编辑了错误的原始帖子。他们是EboMike建议的.theWordLoc是一个向量,我试图使用不需要索引的.push_back方法。但是,我尝试添加索引并收到一组不同的错误。 – fryeguy 2010-12-04 07:06:18

+0

感谢您使用数据库的评论。很诱人。对我来说不幸的是,这是一个关于数据结构的研究项目的一部分,并且我正在使用一个数据库,我只是从头开始构建它......但这点仍然很好。我完全同意。 – fryeguy 2010-12-04 07:21:16

+0

你应该真的考虑它。听起来这实在是你应该使用的。顺便说一句,将编辑关于编译错误的答案 - theLoc不是一个向量,所以你不能说“push_back”。 – EboMike 2010-12-04 07:35:42

1

testProps.theWordLoc.theLoc你指的是一个矢量theWordLoctheLoc成员。这简直是​​不可接受的。你应该使用类似testProps.theWordLoc[0].theLoc的东西。