2010-06-25 15 views
3

我已經接近編程我的大學課程的程序的結束,但我陷入了最後一個麻煩:格式化輸出!一個棘手的計數器 - 只是一個整數,但他不會按要求工作

這與代碼無關:這只是化妝品(但我需要修復,導致我的輸出必須遵守一些標準)。

基本上,我的程序將讀取一個.csv文件,並通過將生成的圖形劃分爲簇來處理該文件。無論如何,我都希望讓它們編號(只要數字是正確的,我不在乎它是以2開始而不是1開始)。問題是我不知道如何做到這一點o-o

我已經有了一個適當的數據結構來處理我的聚類活動:圖的每個節點指向一些列表。如果我有n個集羣,我會有n個列表。這些列表是在Vertex類中實例化的,算法會在運行時合併它們,所以我不能像它們那樣簡單地給它們編號,如果它們在向量中或其他地方。

所以現在我有,比如n個列表。我可以通過查看第一個頂點(它們在向量V中)並跟隨指針輕鬆地啓動。 這樣做後,我在第一個列表中。我可以繼續前進,讓每個節點都指向該列表,並且我希望我的程序在地圖上放置一個「1」。 然後,我會在V的內部查找列表與之前不同的另一個頂點,並再次進入列表並用「2」保存節點,依此類推。

我的計數器不能正常工作:要求在.csv文件3個集羣,輸出是這樣的:

c 1 1 
c 2 1 
c 3 1 
c 4 1 
c 5 1 
c 6 5 
c 7 1 
c 8 1 
c 9 1 
c 10 1 
c 11 1 
c 12 5 
c 13 5 
c 14 1 
c 15 1 
c 16 1 
c 17 1 
c 18 17 
c 19 17 
c 20 17 
c 21 17 
c 22 5 
c 23 17 
c 24 17 
c 25 17 
c 26 17 
c 27 17 
c 28 17 
c 29 17 
c 30 17 
c 31 5 
c 32 5 

集羣標籤是1, 5, 17,而不是1, 2, 3笑U_U

map<int, int> clusterProcessor(Grafo &G, map_t &Map, int clusters, vector<Vertex*> &V { 
    map<int, int> clustermap; 
    list<Vertex*>::iterator listiter; 
    int j; 
    int counter = 1; 
    for (j=1; j < V.size(); ++j) { 
     if (V[j]->getMyset()->front() != V[j-1]->getMyset()->front()) 
      counter++; 
     for (listiter = V[j]->getMyset()->begin(); listiter != V[j]->getMyset()->end(); 
       listiter++) 
      clustermap.insert(pair<int, int> ((*listiter)->id, counter)); 
    } 
    return clustermap; 
} 

void clusterPrinter(map<int, int> m) { 
    map<int, int>::iterator i; 
    for (i = m.begin(); i != m.end(); i++) 
     cout << "c " << i->first << " " << i->second << endl; 
} 
+0

什麼是 「反」?該片段中沒有聲明這樣的變量。 – user168715 2010-06-25 20:59:52

+2

我敢肯定,無論你怎麼努力,你都無法從int中得到序列「1,2,3,lol,U_U」。 – 2010-06-25 21:16:22

+0

@Tyler:我在你評論時爲他修正了一些格式。 – 2010-06-25 21:17:07

回答

1

請注意,數字始終是行出現之前的行號。這表明counter++始終在執行。

果然,它是。

也許你打算比較每個集羣的第一個元素的值,而不是迭代器地址?

根據您建立後續的映射,我建議改變

if (V[j]->getMyset()->front() != V[j-1]->getMyset()->front()) counter++; 

if (V[j]->getMyset()->begin()->id != V[j-1]->getMyset()->begin()->id) counter++; 
+0

Uhm嘗試過,但它不起作用。如果((* V [j] - > getMyset() - > begin())( )輸出是一樣的:( (我不得不像這樣編輯它,因爲我有指針,即使在列表中): if ) - > id!=(* V [j-1] - > getMyset() - > begin()) - > id)counter ++; 我跟隨了Karmastan的建議,並取得了很好的結果,但它的缺陷仍然存在> _> – Tex 2010-06-26 06:50:46

1

只要嘗試在輸出它們時對您的頂點進行重新編號。保留你之前見過的verticies的地圖和他們的新號碼。在打印頂點號碼之前,如果您之前看過,只需打印您已經輸入的新號碼。如果它是新建的,請爲其分配一個新號碼,在地圖上記錄該號碼並使用其新號碼。

0

好的,有更好的東西。我只保留了一個分配給它的布爾向量,而不是另一張地圖。

它的工作完美,我仍然有一個微型錯誤,我無法擺脫。我會粘貼你的代碼和輸出。

代碼:

map<int, int> clusterProcessor(Grafo &G, map_t &Map, int clusters, vector<Vertex*> &V) { 
map<int, int> clustermap; 
list<Vertex*>::iterator listiter; 
int j; 
int counter = 1; 
vector<bool> checkvertex(V.size(), false); // Got 1 bool per each vertex. When I visit it, it'll be set "true". 
for (j=1; j < V.size() ; ++j) { 
    //if (V[j]->getMyset()->front() != V[j-1]->getMyset()->front()) count++; 
     if (checkvertex[j] == false) { 
      for (listiter = V[j]->getMyset()->begin(); listiter != V[j]->getMyset()->end(); listiter++) 
      { 
       clustermap.insert(pair<int, int> ((*listiter)->id, counter)); 
       checkvertex[(*listiter)->id-1] = true; 
      } 
     counter++; 
     } 
} 
return clustermap; 
} 

輸出: C 1 1 的C 2 1 的C 3 1 -C 4 1 的C 5 1 C 6 2 的C 7 1 -C 8 1 C 9 1 C 10的1 C++ 11 1 C 12 2 -C 13 2 -C 14 1 C 15的1 至c 16 1 C 17 1 -C 18 3 C 19 3 C 20的3 C 21 3 -C 22 2 C 23 3 -C 24 3 Ç25 3 C 26 3 C 27 3 C 28 3 Ç 29 3 c 30 3 c 31 2 c 32 2

這就是我想要的,但我試圖檢查它的極限。當我問他32個集羣時會發生什麼:第一個頂點剛剛消失! OO

的C 2 1 的C 3 2 -C 4 3 的C 5 4 C 6 5 -C 7 6 -C 8 7 C 9 8 -C 10 9 C++ 11 10 C 12 11 Ç 13 12 C 14 - 13 C 15的14 -C 16 15 C 17 16 C 18的17 C 19 18 C 20的19 C 21 20 -C 22 21 C 23 22 -C 24 23 Ç25 24 C 26 25 C 27 26 C 28 27 C 29 28 C 30的29 C 31 30 C 32 31

有趣的事實是,它不會僅在其極限發生:對於這個文件,它會發生任何大於13的數字(13仍然沒問題)。

+0

終於有了這個工作! :D j從1開始! (沒有編輯在第一個vesion和新的之間的段落)。 j從0開始一切正常)= – Tex 2010-06-26 15:35:04

相關問題