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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4352333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:58:21  来源:igfitidea点击:

can I build a vector of vectors of structs with vectors of structs? (yes, really)

c++xcodedata-structuresvectorstruct

提问by fryeguy

I am attempting to build a relatively complex data structure (for me). My goal is to read words from text documents and index the words and some specific properties into a hash table. The table is constructed from a vector of vectors of structs: (vector < vector > vecName;). This much I have had luck with. Each unique word is hashed into an index location in the vector. The second dimension of the the vector (the vector of structs) stores info about the file being read and where the word is found in the file. For each file that I read, if I find a certain word multiple times, a count is incremented in the struct and a vector of structs with integers stores the info for all the locations that the word is stored in the file.

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

I have two items I would like assistance with:

我有两个项目需要帮助:

  1. I'm curious if anyone has suggestions for a better data structure implementation than my suggestion. Would a class that contains some independent data members instead of this behemoth possibly be more useable?
  2. It appears that I have either a syntax error that is causing a compilation error or I am simply attempting to build a structure that the vector class doesn't support.
  1. 我很好奇是否有人有比我的建议更好的数据结构实现的建议。包含一些独立数据成员而不是这个庞然大物的类可能更有用吗?
  2. 看来我有一个导致编译错误的语法错误,或者我只是试图构建一个向量类不支持的结构。

Here are the cmpilation errors. All three errors refer to the vector of structs inside a struct:

这是编译错误。所有三个错误都涉及结构内的结构向量:

'class std::vector >' has no member named 'theLoc'
'class std::vector >' has no member named 'theStart'
'class std::vector >' has no member named 'theEnd'

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

If I tweak the code as EboMike suggests, the original errors go away but I then get:

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

I get a different error that I can't post becase the editor thinks I'm posting hyperlinks. The summary is: *'Request for member 'push_back' in 'testProps.wordProps::theWordLoc:theLoc, which is of non-class type 'int'*

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

Here is my code and a link to a diagram (from my blog) of how I see the data structure:

这是我的代码和指向我如何看待数据结构的图表的链接(来自我的博客):

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

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

采纳答案by Victor Parmar

I don't know about design choice of the data structure apart from making it a hasmap but your code is almost correct!

除了使它成为hasmap之外,我不知道数据结构的设计选择,但您的代码几乎是正确的!

Check out my comments:

看看我的评论:

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

回答by EboMike

The compile error first: You're probably referring to this line:

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

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

theWordLoc is a vector, so you'll need to treat it as such, for example:

theWordLoc 是一个向量,所以你需要这样对待它,例如:

testProps.theWordLoc[0].theLoc = 200;

or, if there's nothing there yet:

或者,如果那里还什么都没有:

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

As to your actual question: Is that a viable solution? Yes, it is. However, how much data do you expect to get? And how persistent is it? If the answer is "tons, long", I'd go for a database instead. Have a table for worldLoc, one for wordProps, one for the higher-level vectors, and things are a lot faster and cleaner.

至于您的实际问题:这是一个可行的解决方案吗?是的。但是,您希望获得多少数据?它有多坚持?如果答案是“吨,长”,我会去寻找一个数据库。有一张用于 worldLoc 的表,一张用于 wordProps 的表,一张用于更高级别的向量,事情会变得更快更干净。

Also, I don't like the top-level vectors. I don't understand the structure you intend to do there (I just glanced at the diagram), but it sounds like you're looking for a hashmap instead.

另外,我不喜欢顶级向量。我不明白你打算在那里做的结构(我只是看了一眼图表),但听起来你正在寻找一个哈希图。

回答by Ryan Li

In testProps.theWordLoc.theLocyou are referring to the theLocmember of a vector theWordLoc. This is simply unacceptable. You should use something like testProps.theWordLoc[0].theLoc.

testProps.theWordLoc.theLoc您指theLoc的是 vector的成员theWordLoc。这简直不能接受。你应该使用类似的东西testProps.theWordLoc[0].theLoc

回答by mPopp

Maybe for the data structure a multimap could be your friend here replacing the top lvl vector of vectors.

也许对于数据结构,多图可能是您的朋友,在这里替换向量的顶级 lvl 向量。

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

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