作者ucrxzero (RX-0)
看板C_and_CPP
标题Re: [问题] sort vector 问题
时间Tue Oct 27 02:14:25 2020
第一点 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;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 43.248.19.192 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1603736068.A.5F5.html
※ 编辑: ucrxzero (43.248.19.192 台湾), 10/27/2020 02:46:55