2010-12-04 124 views
3

我試圖建立一個相對複雜的數據結構(對我來說)。我的目標是從文本文檔中讀取單詞,並將單詞和一些特定屬性編入一個散列表。該表由結構向量構成:(向量<向量> vecName;)。我很幸運。每個唯一字被散列到向量中的索引位置。矢量的第二維(結構向量)存儲有關正在讀取的文件的信息以及文件中找到的單詞的位置。對於我讀取的每個文件,如果我多次查找某個單詞,則會在結構中增加一個計數,並且帶有整數的結構向量將存儲單詞存儲在文件中的所有位置的信息。我可以用結構向量構建一個結構向量向量嗎? (是的,真的)

我有兩個項目,我想與援助:

  1. 我很好奇,如果任何人有更好的數據結構實現比我建議的建議。包含一些獨立數據成員而不是這個龐然大物的類可能更有用嗎?
  2. 看來我的語法錯誤是導致編譯錯誤,或者我試圖構建一個向量類不支持的結構。

這裏是cmpilation錯誤。所有這三個錯誤是指結構的載體結構內:

「類的std ::矢量>」沒有名爲「theLoc」
「類的std ::矢量>」沒有名爲「theStart」成員成員
「類的std ::矢量>」沒有名爲成員「附帶一份最終」

如果我調整代碼爲EboMike表明,原來的錯誤消失,但後來我得到:

我得到一個不同的錯誤我不能發佈,因爲編輯認爲我發佈超鏈接。總結如下:*'請求'testProps.wordProps :: theWordLoc:theLoc中的成員'push_back',它是非類類型'int'*

這裏是我的代碼和一個圖的鏈接

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

「看來,我要麼就導致一個編譯錯誤語法錯誤」 ......請張貼錯誤消息的全文。 – 2010-12-04 06:48:49

回答

1

我不知道數據結構的設計選擇,除了使它成爲一個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; 
} 
+0

謝謝Vic!這非常合理,我在將TestProps傳遞給WordProps時使用了相同類型的邏輯,但它在那裏有點霧,並且我沒有看到將wl傳遞給wordProps的相同模式。感謝您幫助這個小小的受害者! :)現在我可以休息一下了! – fryeguy 2010-12-04 07:32:41

3

編譯錯誤第一:你可能指的是這一行:我的我怎麼看的數據結構博客)

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

theWordLoc是AV埃克特,所以你需要把它當作這樣的,例如:

testProps.theWordLoc[0].theLoc = 200; 

或者,如果有什麼都沒有尚未:

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

至於你的實際問題:是一個可行的解決方案?是的。但是,您希望得到多少數據?它有多持久?如果答案是「噸,長」,我會去一個數據庫,而不是。有一個worldLoc的表格,一個用於wordProps,一個用於更高級別的矢量,而且事情要快得多,乾淨得多。

此外,我不喜歡頂級向量。我不明白你打算在那裏做的結構(我只是看了一下圖表),但是這聽起來像你在尋找一個hashmap。

+0

我編輯了錯誤的原始帖子。他們是EboMike建議的.theWordLoc是一個向量,我試圖使用不需要索引的.push_back方法。但是,我嘗試添加索引並收到一組不同的錯誤。 – fryeguy 2010-12-04 07:06:18

+0

感謝您使用數據庫的評論。很誘人。對我來說不幸的是,這是一個關於數據結構的研究項目的一部分,並且我正在使用一個數據庫,我只是從頭開始構建它......但這點仍然很好。我完全同意。 – fryeguy 2010-12-04 07:21:16

+0

你應該真的考慮它。聽起來這實在是你應該使用的。順便說一句,將編輯關於編譯錯誤的答案 - theLoc不是一個向量,所以你不能說「push_back」。 – EboMike 2010-12-04 07:35:42

1

testProps.theWordLoc.theLoc你指的是一個矢量theWordLoctheLoc成員。這簡直是​​不可接受的。你應該使用類似testProps.theWordLoc[0].theLoc的東西。