作者yhn0tgb60 (呦厚厚)
看板C_and_CPP
标题Re: [STL ] map 会自动在最前面加空资料???
时间Sun Aug 23 13:28:06 2009
我刚刚试了一个方法
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
1F:→ kazuto:用vector 然後自己sort呢? map不太适合随机取值 08/23 14:45