: 网络上好像是说需要把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