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

看板C_and_CPP (C/C++)作者 (呦厚厚)時間16年前 (2009/08/23 13:28), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串2/3 (看更多)
我剛剛試了一個方法 map <DWORD, Node>::iterator buf; buf=closelist.begin(); 用 buf來進行運算 但是假如我想取的是map第5個陣列值 我用 buf=closelist.begin()+5; complie不會過 難道我要 buf++; 5次嗎??? 請問有沒有更有效率的辦法 find() 是搜尋key 但我現在要用的是取出陣列的某個位置 這有點不符合我的需求 麻煩大家了 謝謝 ※ 引述《yhn0tgb60 (呦厚厚)》之銘言: : 我宣告了一個 : 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 14:45, , 1F
用vector 然後自己sort呢? map不太適合隨機取值
08/23 14:45, 1F
文章代碼(AID): #1AaDDd3V (C_and_CPP)
文章代碼(AID): #1AaDDd3V (C_and_CPP)