所以我一直在尝试根据字符出现的频率对字符串进行排序。然而,我一直在使用的在线法官向我显示了错误
第 17 行:无效使用非静态成员函数 '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); });
关于c++ - C++ wrt 字符串中的 STL 排序函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176716/