我已經接近編程我的大學課程的程序的結束,但我陷入了最後一個麻煩:格式化輸出!一個棘手的計數器 - 只是一個整數,但他不會按要求工作
這與代碼無關:這只是化妝品(但我需要修復,導致我的輸出必須遵守一些標準)。
基本上,我的程序將讀取一個.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;
}
什麼是 「反」?該片段中沒有聲明這樣的變量。 – user168715 2010-06-25 20:59:52
我敢肯定,無論你怎麼努力,你都無法從int中得到序列「1,2,3,lol,U_U」。 – 2010-06-25 21:16:22
@Tyler:我在你評論時爲他修正了一些格式。 – 2010-06-25 21:17:07