C++ 我可以用结构向量构建结构向量向量吗?(是的,真的)

C++ 我可以用结构向量构建结构向量向量吗?(是的,真的),c++,xcode,data-structures,vector,struct,C++,Xcode,Data Structures,Vector,Struct,我正在尝试(为我)构建一个相对复杂的数据结构。我的目标是从文本文档中读取单词,并将单词和一些特定属性索引到哈希表中。该表由结构的向量构成:(vectorvecName;)。这就是我的运气。每个唯一的单词都散列到向量中的索引位置。向量的第二维度(结构的向量)存储有关正在读取的文件以及在文件中找到单词的位置的信息。对于我读取的每个文件,如果我多次找到某个单词,结构中的计数将递增,带整数的结构向量将存储该单词存储在文件中的所有位置的信息 我有两项需要帮助: 我很好奇是否有人有比我的建议更好的数据结构实

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

我有两项需要帮助:

  • 我很好奇是否有人有比我的建议更好的数据结构实现的建议。一个包含一些独立数据成员的类而不是这个庞然大物可能更有用吗
  • 似乎我有一个导致编译错误的语法错误,或者我只是试图构建一个vector类不支持的结构
  • 以下是C编译错误。这三个错误都是指结构中的结构向量:

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

    如果我按照EboMike的建议调整代码,原始错误会消失,但我会得到:

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

    以下是我的代码和一个图表链接(来自我的博客),该图表显示了我对数据结构的看法:

    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    结构字定位
    {
    int theLoc;//文件中单词的位置
    int theStart;//句子的开头
    int theEnd;//句子的结尾
    };
    struct wordProps//存储要放置在数组中的单词信息
    {
    string theFile;//存储找到单词的文件
    int theCount;//随着单词的每次出现而递增
    vector theWordLoc;//存储单词每次出现时的单词loc信息
    };
    int main()
    {    
    int Tsize=20000;
    wordProps测试道具;
    testProps.theFile=“test1”;
    testProps.theCount=1;
    testProps.theWordLoc.theLoc.push_back(200);
    testProps.theWordLoc.theStart.push_back(1);
    测试道具。单词loc。theEnd。推回(15);
    向量单词道具;
    单词props.resize(Tsize);
    单词props[0]。推回(testProps);
    
    cout首先是编译错误:您可能指的是这一行:

    testProps.theWordLoc.theLoc.push_back(200);
    testProps.theWordLoc.theStart.push_back(1);
    testProps.theWordLoc.theEnd.push_back(15);
    
    单词loc是一个向量,因此您需要将其视为向量,例如:

    testProps.theWordLoc[0].theLoc = 200;
    
    或者,如果那里什么都没有:

    wordLoc wordLocData;
    worldLocData.theLoc = 200;
    worldLocData.theStart = 1;
    worldLocData.theEnd = 15;
    testProps.theWorldLoc.push_back(worldLocData);
    
    至于你的实际问题:这是一个可行的解决方案吗?是的,是的。但是,你希望得到多少数据?它的持久性如何?如果答案是“吨,长”,我会选择一个数据库。有一个worldLoc表,一个wordProps表,一个更高级别的向量表,事情会更快更干净


    另外,我不喜欢顶级向量。我不理解您打算在那里做的结构(我只是浏览了一下图表),但听起来您是在寻找哈希映射。

    除了将数据结构设置为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;
    }
    
    intmain()
    {    
    int Tsize=20000;
    wordProps测试道具;
    testProps.theFile=“test1”;
    testProps.theCount=1;
    //创建wordLoc对象
    wordLoc wl;
    wl.THOLC=200;
    wl.theStart=1;
    wl.theEnd=15;
    //把它放到向量中
    测试道具。单词LOC。推回(wl);
    向量单词道具;
    单词props.resize(Tsize);
    单词props[0]。推回(testProps);
    
    cout在
    testProps.theLoc.theLoc
    中,您指的是向量
    theLoc
    的成员。这是不可接受的。您应该使用类似
    testProps.theWordLoc[0].theLoc

    对于数据结构,多重映射可能是您的朋友,它可以取代向量的顶级lvl向量


    “我似乎有一个语法错误导致编译错误”…请发布错误消息的全文。我编辑了包含错误的原始帖子。它们位于Emomike建议的行。theWordLoc是一个向量,我正在尝试使用不需要索引的.push_back方法。但是,我尝试添加索引,收到了一组不同的错误。感谢您对使用da的评论很不幸。对我来说,这是一个关于数据结构的研究项目的一部分,而且我正在有效地使用一个数据库,我只是从头开始构建它。但是这一点仍然是很好的。我不同意。你应该认真考虑一下。听起来好像是你应该使用的。BTW,将编辑关于C的答案。ompile error-theLoc不是一个向量,所以你不能对它说“push_back”。那么,你是在暗示在这种情况下,vector类的.push_back方法是不可接受的,我需要提供一个索引来访问WordLoc的一个成员吗?@fryeguy:如果你想
    push_back
    ,请使用:
    testProps.theWordLoc.push_back({instance of wordLoc})
    单词loc
    是一个向量。@fryeguy你的逻辑是错误的。你的代码是
    testProps.theWordLoc.theLoc.push_back
    。这意味着“进入
    testProps”
    
    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;
    }