Re: [問題] sort vector 問題

看板C_and_CPP作者 (Han-Yuan)時間3年前 (2020/10/28 00:59), 編輯推噓2(206)
留言8則, 4人參與, 3年前最新討論串6/6 (看更多)
: 網路上好像是說需要把comp改成靜態函數,但還是不知從何下手,請問版上高手,謝謝!! 其實,你很多地方打錯字。你原來的comp並沒有問題。 這裡多塞資料跟印出去確認正確性。 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Info{ float score; float frank; }; bool comp(const Info &Info1, const Info &Infor2){ return Info1.score>Infor2.score; } void MyFunction(){ vector<Info> my; Info _tmp; for(int i=0;i<10;i++) { _tmp.frank = i; _tmp.score = i; my.push_back(_tmp); } cout<<"Before: "; for(vector<Info>::iterator citer = my.begin(); citer != my.end(); ++citer) { cout<<citer->score<<" "; } cout<<endl; sort(my.begin(), my.end(), comp); cout<<"After : "; for(vector<Info>::iterator citer = my.begin(); citer != my.end(); ++citer) { cout<<citer->score<<" "; } cout<<endl; } int main() { MyFunction(); return 0; } 輸出: Before: 0 1 2 3 4 5 6 7 8 9 After : 9 8 7 6 5 4 3 2 1 0 -- 不要問我從哪來,我只是一個浪跡天涯的工程師.... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.8.166 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1603817990.A.CEE.html

10/28 01:14, 3年前 , 1F
所以這就是為什麼大家都要原 PO 直接貼上來而不是轉打...
10/28 01:14, 1F

10/28 01:16, 3年前 , 2F
他的問題大概是 comp 寫怪了導致 "comp" 這名字無法直接用
10/28 01:16, 2F

10/28 01:16, 3年前 , 3F
那這就要看他原本到底 comp 是怎麼寫的了
10/28 01:16, 3F

10/28 01:37, 3年前 , 4F
置底的公告往往是會被遺忘的
10/28 01:37, 4F

10/28 02:39, 3年前 , 5F
置底公告改個標題好了....
10/28 02:39, 5F

10/29 00:17, 3年前 , 6F
版主改了什麼?
10/29 00:17, 6F

10/30 16:20, 3年前 , 7F
發文請儘量附上程式碼
10/30 16:20, 7F

10/30 20:01, 3年前 , 8F
10/30 20:01, 8F
文章代碼(AID): #1Vc586pk (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1Vc586pk (C_and_CPP)