Re: [問題] sort vector 問題

看板C_and_CPP作者 (RX-0)時間3年前 (2020/10/27 02:14), 3年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/6 (看更多)
第一點 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://www.ptt.cc/bbs/C_and_CPP/M.1603736068.A.5F5.html ※ 編輯: ucrxzero (43.248.19.192 臺灣), 10/27/2020 02:46:55
文章代碼(AID): #1Vbn84Nr (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1Vbn84Nr (C_and_CPP)