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