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