Re: [问题] sort vector 问题

楼主: ucrxzero (RX-0)   2020-10-27 02:14:25
第一点 compare function 只有在class里面定义才需要static
因为 sort默认是 用<比的所以拿这个举例
你只会看到别人在class这样定义
class{
bool operator<(Foo const& other) { }

static bool operator<(Foo const& other, Foo const& another) { }
}
而不是
class{
bool operator<(Foo const& other, Foo const& another) { }
}
因为这样会有三个东西
a.operator<(b,c) 等于你比三个欸
static就是没有物件这个主人
顺便科普一下
static member function(用于存取static member)
static member variable (用于counter)
static global function(用于不给别的source拿来include,跟extern相反,Kernel driver常用)
static global variable(用于不给别人拿来用,extern的相反,Kernel driver常用到)
然后static都不能递回
static都要
都放在data segment跟着整个process的生命周期存活
这书上写的
但是没有初始化则放在bss而且都是零(如果像stl的string就是""空字串)
第二点 这样写没报错
#include<vector>
#include<algorithm>
using namespace std;
struct Info{
float score;
float rank;
};
bool comp(const Info &Info1, const Info &Infor2){
return Info1.score>Infor2.score;
}
void MyFunction(){
vector<Info>my;
std::sort(my.begin(), my.end(), comp); //error here
}
int main(){
MyFunction();
return 0;
}

Links booklink

Contact Us: admin [ a t ] ucptt.com