C++ with std::unordered_map and std::sort


  • 5
    J
    class Solution {
    public:
        string frequencySort(string s) {
            unordered_map<char, int> hist;
            for (auto c : s) {
                ++hist[c];
            }
            
            auto pred = [&hist] (char a, char b) {
                return hist[a] > hist[b] || (hist[a] == hist[b] && a > b);
            };
            
            sort(s.begin(), s.end(), pred);
            
            return s;
        }
    };
    

  • 0
    N

    what does

    auto pred = [&hist] (char a, char b) {
                return hist[a] > hist[b] || (hist[a] == hist[b] && a > b);
            };
    
    

    do? I haven't seen the syntax for [&hist] (char a, char b) before.


  • 1
    J

    This is C++ lambda object. Check this for more details:
    http://en.cppreference.com/w/cpp/language/lambda


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.