[問題] priority_queue
遇到的問題: (題意請描述清楚)
想要做一個sort
可是在負數的情況下會 "偶爾"會失敗!!
希望得到的正確結果:
從大排到小!!
程式跑出來的錯誤結果:
會有數字亂跳
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Dev C++
有問題的code: (請善用置底文標色功能)
#include<iostream>
#include<map>
#include<fstream>
#include<time.h>
#include<string>
#include<vector>
#include<queue>
using namespace std;
class gate
{
public:
double gain;
int side;
};
class Greater
{
public:
bool operator()(const gate* L, const gate* R)
{
return (*L).gain < (*R).gain;
}
};
typedef priority_queue <gate*,vector<gate*>,Greater > QQ;
int main()
{
QQ sort0;
int i;
gate* ga;
map<int,gate*> ABC;
map<int,gate*>::iterator it;
for(i=0;i<100;i++)
{
ga=new gate();
ABC[i]=ga;
ga->gain=0;
sort0.push(ga);
}
i=0;
for(it=ABC.begin();it!=ABC.end();it++)
{
it->second->gain=i--; //這裡改成i++ ,sort的結果就是正確的= =""
}
for(i=0;i<100;i++)
{
cout<<sort0.top()->gain<<endl;
sort0.pop();
}
getchar();
return 0;
}
補充說明:
麻煩大家幫忙了QQ
--
☆ ▁▁ ▁▁ ▁ ▁▁
╔══╦══╦═╦╦╦═╗ ▏█ █ ╱█ █
║ ═╣ ═╣╔╗║╔╗║ ▏█ █ █ ▁█
╠═ ║ ═╣╚╝║║║║ ▏█ █ █ █ ~ Sean0712 ~
╚══╩══╩═╩╩╝╚╝ ██ █ ▁█▁ █▁﹏﹏﹏﹏
0 Ⅶ Ⅰ Ⅱ ξicebox
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.201.210
※ 編輯: sean0712 來自: 140.113.201.210 (04/14 20:44)
→
04/14 20:47, , 1F
04/14 20:47, 1F
→
04/15 02:19, , 2F
04/15 02:19, 2F
→
04/16 15:09, , 3F
04/16 15:09, 3F
→
04/16 15:09, , 4F
04/16 15:09, 4F
→
04/16 15:10, , 5F
04/16 15:10, 5F
推
04/16 15:44, , 6F
04/16 15:44, 6F
→
04/16 15:44, , 7F
04/16 15:44, 7F
→
04/16 15:45, , 8F
04/16 15:45, 8F