所以我一直在尝试根据字符的频率对字符串进行排序。然而我一直使用的在线法官向我显示了错误
Line 17: invalid use of non-static member function 'bool olution::helper(char, char)'
为什么对我的函数的调用是错误的?我已经用过sort()
函数之前,但不是字符串。是我的helper()
功能不正确?
class Solution {
public:
unordered_map<char,int> freq;
bool helper(char c1,char c2){
if(freq[c1]>freq[c2]) return false;
else return true;
}
string frequencySort(string s) {
for(char c:s)
{
freq[c]++;
}
sort(s.begin(),s.end(),helper);
return s;
}
};
使用 lambda 捕获this
:
sort(s.begin(),s.end(),[this](auto a, auto b) -> bool { return helper(a,b); });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)