我可以用结构向量构建一个结构向量的向量吗? (对真的)

时间:2010-12-04 06:43:36

标签: c++ xcode data-structures vector struct

我正在尝试构建一个相对复杂的数据结构(对我而言)。我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。该表由结构的向量矢量构成:(vector< vector> vecName;)。我很幸运。每个唯一的单词都被散列到向量中的索引位置。向量的第二个维度(结构的向量)存储有关正在读取的文件以及在文件中找到该单词的信息。对于我读取的每个文件,如果我多次找到某个单词,则在结构中递增计数,并且带有整数的结构向量存储该单词存储在文件中的所有位置的信息。

我有两件我想要帮助的项目:

  1. 我很好奇是否有人建议更好的数据结构实施而不是我的建议。包含一些独立数据成员而不是这个庞然大物的类可能更有用吗?
  2. 看来我有一个导致编译错误的语法错误,或者我只是试图构建一个vector类不支持的结构。
  3. 以下是cmpilation错误。所有三个错误都引用结构中的结构向量:

    'class std :: vector>'没有名为'theLoc'的成员 'class std :: vector>'没有名为'theStart'的成员 'class std :: vector>'没有名为'theEnd'的成员

    如果我按照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;
    }
    

4 个答案:

答案 0 :(得分:3)

首先是编译错误:你可能是指这一行:

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。

答案 1 :(得分: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;
}

答案 2 :(得分:1)

testProps.theWordLoc.theLoc中,您指的是向量theLoc的{​​{1}}成员。这简直是​​不可接受的。您应该使用类似theWordLoc的内容。

答案 3 :(得分:0)

也许对于数据结构,多图可以成为你的朋友,取代矢量的顶级lvl向量。

  

http://www.cplusplus.com/reference/stl/multimap/