C ++ WRT字符串中的STL排序功能 [英] STL sort function in C++ wrt strings

查看:113
本文介绍了C ++ WRT字符串中的STL排序功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试根据字符频率对字符串进行排序.但是我一直在使用的在线法官向我显示错误
Line 17: invalid use of non-static member function 'bool olution::helper(char, char)'
为什么对我的函数的调用是错误的?我以前使用过sort()函数,但没有使用过字符串.我的helper()函数不正确吗?

So I've been trying to sort a string based on the frequency of its characters. However the online judge I've been using shows me the error
Line 17: invalid use of non-static member function 'bool olution::helper(char, char)'
Why is the call to my function wrong? I have used the sort() function before, but not to strings. Is my helper() function incorrect?

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:

Use a lambda to capture this:

sort(s.begin(),s.end(),[this](auto a, auto b) -> bool { return helper(a,b); });

这篇关于C ++ WRT字符串中的STL排序功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆