看板 C_and_CPP 關於我們 聯絡資訊
各位好 小弟C++菜逼八 今天寫leetcode30天的時候遇到sort函數的問題搞不懂是為什麼所以上來發問,請各位指點迷津 這是今天leetcode的題目 : https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3393/ 我的解法是: 1. 用map<int 數字,int 頻率>統計頻率 2. map內丟到vector<pair<int 數字,int 頻率>>的num_freq 3. 對num_freq做vector<pair<int,int>>的排序(依照pair->second做大到小排序) 先講結論跟想問的,後面再補code and ref //問題點如下 問題出在第三步對pair做排序時 法一在leetcode上run compile error,但是在codeblock運行ref的範例沒問題 改成法二leetcode上就可以跑了 想知道為什麼法一要改成法二才可以? //code and reference 法1:ref https://www.geeksforgeeks.org/sorting-vector-of-pairs-in-c-set-1-sort-by-first-and-second/ 法1執行結果如截圖http://i.imgur.com/Su0ou9f.jpg http://i.imgur.com/qB2mSsB.jpg
法2:ref https://www.itread01.com/content/1544616191.html 法2執行結果如截圖http://i.imgur.com/kQlU7tU.jpg
----- Sent from JPTT on my Xiaomi MI 9. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.55.219 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1594978788.A.C80.html ※ 編輯: wilson50101 (101.12.55.219 臺灣), 07/17/2020 17:41:02 ※ 編輯: wilson50101 (101.12.55.219 臺灣), 07/17/2020 17:42:05
wilson50101: 法1 ref連結斷掉了抱歉 07/17 17:42
nh60211as: 加static啊 07/17 18:05
hsnuyi: google "why std sort static compare" 07/17 20:46
Dracarys: 用lambda expression? 07/18 00:03
LPH66: 不是 static 的成員函數有一個隱藏的 this 參數 07/18 08:39
LPH66: sort 的比較函數無法使用這種還有額外需求的函數 07/18 08:40
poyenc: 這是因為 call syntax 還沒有 unify 的緣故, 看回覆晚點補 07/18 09:57
poyenc: 充 07/18 09:57
saladim: "static 的成員函數有一個隱藏的 this 參數" 請問這邊有 08/11 03:06
saladim: 少字嗎? 看不太懂..... 08/11 03:06
Lipraxde: 因為你看漏了 「不是」 08/12 02:42