vendredi 19 juillet 2019

How should I correct this priority_queue comparison function?

With a number array nums, I'd like to sort the unique numbers by the frequency of their occurrences. The compiler is complaining about passing the unordered_map as 'this' argument discards qualifiers. How do I fix this?

    void sortByFreq(const vector<int>& nums) {
      unordered_map<int, int> counts;
      for (auto i: nums) ++counts[i];
      auto byCount = [counts](const int& a, const int& b) { return counts[a] > counts[b]; };
      priority_queue<int, vector<int>, decltype(byCount)> minFreq(byCount);
      for (auto& kv: counts) {
        minFreq.push(kv.first);
      }
      ......
    }

Aucun commentaire:

Enregistrer un commentaire