开发者

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

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.

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.

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'

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

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'*

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

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


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:

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.

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.


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

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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜