我能否使用结构体的向量创建结构体向量的向量?(是的,真的)

3
我正在尝试构建一个相对复杂的数据结构(对我来说)。我的目标是从文本文档中读取单词并将单词和一些特定属性索引到哈希表中。该表是由一个矢量的矢量的结构体构成的:(vector < vector > vecName;)。这个我已经做到了。每个唯一的单词都被散列成向量中的索引位置。向量的第二维(结构体的向量)存储有关正在读取的文件和单词在文件中的位置的信息。对于我读取的每个文件,如果我发现某个单词多次出现,则在结构体中递增计数,并且带有整数的结构体向量存储单词存储在文件中的所有位置的信息。
我有两个需要协助的项目:
1. 我想知道是否有人有比我的建议更好的数据结构实现建议。包含一些独立数据成员的类是否可能更可用? 2. 看起来我要么有语法错误导致编译错误,要么我只是试图构建一个向量类不支持的结构。
以下是编译错误。所有三个错误都涉及结构体内部的结构体向量:

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

如果我像EboMike建议的那样调整代码,原始错误就消失了,但是我接着会得到:

我得到了一个不同的错误,无法发布,因为编辑器认为我在发布超链接。总结是:*'testProps.wordProps :: theWordLoc:theLoc'中请求非类类型'int'的'member push_back'*

这是我的代码和一个数据结构图的链接(来自我的博客):

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
似乎我有一个语法错误导致编译错误...请发布完整的错误消息文本。 - Laurence Gonsalves
4个回答

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创建一个表格,为更高级别的向量创建一个表格,这样速度更快,更清晰。
另外,我不喜欢顶级向量。我不理解您打算在那里执行的结构(我只是瞥了一眼图表),但这听起来像您正在寻找哈希映射。

我编辑了原始帖子中的错误。它们在 EboMike 建议的行上。theWordLoc 是一个向量,我正在尝试使用不需要索引的 .push_back 方法。但是,我尝试添加索引并收到了一组不同的错误。 - fryeguy
感谢您对使用数据库的评论。这很诱人。不幸的是,对于我来说,这是数据结构研究项目的一部分,实际上我正在使用数据库,只是从头开始构建它...但重点仍然很好地被接受了。我非常同意。 - fryeguy
你真的应该考虑一下。听起来这确实是你应该使用的东西。顺便说一下,我会编辑关于编译错误的答案 - theLoc不是一个向量,所以你不能在它上面使用“push_back”。 - EboMike

1
testProps.theWordLoc.theLoc 中,您正在引用向量 theWordLoc 的成员 theLoc。这是完全不可接受的。您应该使用类似于 testProps.theWordLoc[0].theLoc 的东西。

那么您是在暗示在这种情况下,向量类的.push_back方法是不可接受的,我需要提供一个索引来访问theWordLoc的成员吗? - fryeguy
@fryeguy:如果你想要使用 push_back,请使用以下代码:testProps.theWordLoc.push_back({wordLoc 实例})theWordLoc 是一个向量。 - Ryan Li
@fryeguy,你的逻辑方向错了。你的代码写成了testProps.theWordLoc.theLoc.push_back。这意味着“进入testProps,然后找到其中的theWordLoc成员,然后找到那个theLoc成员,然后找到那个push_back成员”。这段代码试图在theWordLoc中查找theLoc,但是theWordLoc是一个std::vector - 它没有theLoc。拥有theLoc的是你想要将其push_back的东西。你想要将其push_backtheWordLoc上,所以你的代码应该像这样:theWordLoc.push_back(这里放置一些内容) - Karl Knechtel

1

除了将其作为哈希映射之外,我不知道数据结构的设计选择,但是你的代码几乎正确!

请查看我的评论:

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;
}

谢谢Vic!这很有道理,当我将testProps传递给wordProps时,我正在使用相同类型的逻辑,但在那里变得有点模糊,而我未能看到传递wl给wordProps的相同模式。感谢您帮助我取得这个小胜利!现在我可以休息一下了! :) - fryeguy

0

我能否使用哈希函数在multimap中存储值?我想要创建一个尽可能接近O(1)的表格。 - fryeguy
从我发布的链接中,您可以看到在使用multimap模板时,您可以将Comparer作为参数提供,其中可以包含您的哈希函数(如果我没有完全错过什么)。插入应该类似于O(log(n)),而哈希查找则为O(1)。 - mPopp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接