[問題] priority_queue

看板C_and_CPP作者 (Winds~)時間15年前 (2010/04/14 20:39), 編輯推噓1(107)
留言8則, 4人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 想要做一個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
或許可以自己用 debugger 觀察看看
04/14 20:47, 1F

04/15 02:19, , 2F
對阿,很簡單的東西,自己 debugger 開來看看
04/15 02:19, 2F

04/16 15:09, , 3F
嗯!!真的好奇怪!! try出來看看還是不曉得為什麼!
04/16 15:09, 3F

04/16 15:09, , 4F
似乎只有正數的排列比較不會有問題~~
04/16 15:09, 4F

04/16 15:10, , 5F
不曉得有沒有人可以幫我看一下!! 我真的try不出來了!!
04/16 15:10, 5F

04/16 15:44, , 6F
你是怎麼try的?? 除了try以外, 1/2F應該是建議你自己學
04/16 15:44, 6F

04/16 15:44, , 7F
著step by step debug, 搭配watch或print message來查到
04/16 15:44, 7F

04/16 15:45, , 8F
底程式哪裡有誤, 而不是只是換換數字 try 喔....@_@"
04/16 15:45, 8F
文章代碼(AID): #1BnRU0J5 (C_and_CPP)