c++ - C++ wrt 字符串中的 STL 排序函数

标签 c++ sorting stl stl-algorithm

所以我一直在尝试根据字符出现的频率对字符串进行排序。然而,我一直在使用的在线法官向我显示了错误
第 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/

相关文章:

c++ - 如何在 Gtk 中创建带有图像的组合框?

jquery - 有没有办法在 JQuery Datatable 中的标题文本旁边显示排序图标?

c++ - STL maps中的key是如何升序排列的?

c++ - 动态创建的作用域守卫

c++ - 如何用无限循环停止QThread

c++ - 通过线程本地存储访问 shared_ptr

php - 首先从特定值开始对数据库结果进行排序 Kohana PHP

python - 排序字典列表时如何使用 operator.itemgetter 忽略 None 值?

c++ - 使用 "approximate"STL 贴图

C++ STL vector 不接受复制构造函数