[問題] C++ sort函數在leetcode上使用問題

看板C_and_CPP作者 (八卦肥宅)時間3年前 (2020/07/17 17:39), 3年前編輯推噓2(209)
留言11則, 8人參與, 3年前最新討論串1/2 (看更多)
各位好 小弟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

07/17 17:42, 3年前 , 1F
法1 ref連結斷掉了抱歉
07/17 17:42, 1F

07/17 18:05, 3年前 , 2F
加static啊
07/17 18:05, 2F

07/17 20:46, 3年前 , 3F
google "why std sort static compare"
07/17 20:46, 3F

07/18 00:03, 3年前 , 4F
用lambda expression?
07/18 00:03, 4F

07/18 08:39, 3年前 , 5F
不是 static 的成員函數有一個隱藏的 this 參數
07/18 08:39, 5F

07/18 08:40, 3年前 , 6F
sort 的比較函數無法使用這種還有額外需求的函數
07/18 08:40, 6F

07/18 09:57, 3年前 , 7F
這是因為 call syntax 還沒有 unify 的緣故, 看回覆晚點補
07/18 09:57, 7F

07/18 09:57, 3年前 , 8F
07/18 09:57, 8F

08/11 03:06, 3年前 , 9F
"static 的成員函數有一個隱藏的 this 參數" 請問這邊有
08/11 03:06, 9F

08/11 03:06, 3年前 , 10F
少字嗎? 看不太懂.....
08/11 03:06, 10F

08/12 02:42, 3年前 , 11F
因為你看漏了 「不是」
08/12 02:42, 11F
文章代碼(AID): #1V4N7ao0 (C_and_CPP)
文章代碼(AID): #1V4N7ao0 (C_and_CPP)