作者wilson50101 (八卦肥宅)
看板C_and_CPP
标题[问题] C++ sort函数在leetcode上使用问题
时间Fri Jul 17 17:39:44 2020
各位好 小弟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://webptt.com/cn.aspx?n=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
1F:→ wilson50101: 法1 ref连结断掉了抱歉 07/17 17:42
2F:→ nh60211as: 加static啊 07/17 18:05
3F:→ hsnuyi: google "why std sort static compare" 07/17 20:46
4F:推 Dracarys: 用lambda expression? 07/18 00:03
5F:推 LPH66: 不是 static 的成员函数有一个隐藏的 this 参数 07/18 08:39
6F:→ LPH66: sort 的比较函数无法使用这种还有额外需求的函数 07/18 08:40
7F:→ poyenc: 这是因为 call syntax 还没有 unify 的缘故, 看回覆晚点补 07/18 09:57
8F:→ poyenc: 充 07/18 09:57
9F:→ saladim: "static 的成员函数有一个隐藏的 this 参数" 请问这边有 08/11 03:06
10F:→ saladim: 少字吗? 看不太懂..... 08/11 03:06
11F:→ Lipraxde: 因为你看漏了 「不是」 08/12 02:42