Re: [問題] sort vector 問題

作者: MasterChang (我愛ASM)   2020-10-28 00:59:47
: 網路上好像是說需要把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
作者: LPH66 (-6.2598534e+18f)   2020-10-28 01:14:00
所以這就是為什麼大家都要原 PO 直接貼上來而不是轉打...他的問題大概是 comp 寫怪了導致 "comp" 這名字無法直接用那這就要看他原本到底 comp 是怎麼寫的了
作者: firejox (Tangent)   2020-10-28 01:37:00
置底的公告往往是會被遺忘的
作者: wtchen (沒有存在感的人)   2020-10-28 02:39:00
置底公告改個標題好了....
作者: ucrxzero (RX-0)   2020-10-29 00:17:00
版主改了什麼?
作者: wtchen (沒有存在感的人)   2020-10-30 16:20:00
發文請儘量附上程式碼
作者: ucrxzero (RX-0)   2020-10-30 20:01:00
作者: goodzey (--)   2020-11-02 11:31:00
建設性回覆,謝謝!!

Links booklink

Contact Us: admin [ a t ] ucptt.com