[STL ] map 會自動在最前面加空資料???

看板C_and_CPP (C/C++)作者 (呦厚厚)時間16年前 (2009/08/23 03:46), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/3 (看更多)
我宣告了一個 STL的map 裡面的資料是擺一個struct 而我每加一筆資料到map裡時 會用minHeap排序 依照struct 裡面的某個float 的大小排序 可是當我在比較map中的某兩個位置的 struct 中的某個float 的大小時 系統卻會常常會自動在我的map陣列的最前面加入兩個或一個都是 0 的資料 而且之後也不會自動刪除 以下是我的部份程式碼 struct Node { int verID1; int verID2; float curCost; float heuristic; Node *parent; }; inline DWORD GetKey( int verID1, int verID2 ) { return ( MAKELONG( verID1, verID2 )); } inline int GetHeapParent( int i ) { return ((i+1)/2-1); } map <DWORD, Node> closelist; void Astar::InsertMinHeap( Node n) { closelist.insert( map< DWORD, Node>:: value_type( GetKey( n.verID1 ,n.verID2 ), n)); int curPose=closelist.size()-1; while(TRUE) { int i=GetHeapParent( curPose ); if(i==-1) break; if((closelist[curPose].curCost+closelist[curPose].heuristic)< (closelist[i].curCost+closelist[i].heuristic)) { Node buf=closelist[curPose]; closelist[curPose]=closelist[i]; closelist[i]=buf; curPose=i; } else break; } } 就是下面這兩行 "" if((closelist[curPose].curCost+closelist[curPose].heuristic)< (closelist[i].curCost+closelist[i].heuristic)) "" 常常會都自動再幫我加兩個或一個 資料都為0的map 而且加在最前面 希望大家可以看的懂 麻煩大家了 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.248.163.42

08/23 08:11, , 1F
取資料請不要使用[]下摽來取 請用find 侯捷STL一書有說
08/23 08:11, 1F

08/23 13:10, , 2F
謝謝 有一個方向了
08/23 13:10, 2F

08/23 13:18, , 3F
但是 find裡面放的是 key 但我想取的是陣列位置
08/23 13:18, 3F

08/23 13:21, , 4F
那應該怎麼做 ???
08/23 13:21, 4F
文章代碼(AID): #1Aa4huQ6 (C_and_CPP)
文章代碼(AID): #1Aa4huQ6 (C_and_CPP)