I am trying to create a functor as custom comparator for my priority_queue which takes in an unordered_map as a parameter for constructor. I am not sure how to call the functor when declaring the priority_queue as I am getting the error:
"Line 22: Char 48: error: template argument for template type parameter must be a type priority_queue<string, vector, compareWords(wordFreq)> pq;"
class compareWords{
public:
compareWords(unordered_map<string, int> &wordCounts): wordFreq(wordCounts){}
bool operator()(string word1, string word2){
if(wordFreq[word1] == wordFreq[word2]){
return word1 < word2;
}
return wordFreq[word1] < wordFreq[word2];
}
private:
unordered_map<string, int> wordFreq;
};
vector<string> topKFrequent(vector<string>& words, int k) {
vector<string> result;
unordered_map<string, int> wordFreq;
for(int i = 0; i < words.size(); i++){
wordFreq[words[i]]++;
}
priority_queue<string, vector<string>, compareWords(wordFreq)> pq;
for(auto& item: wordFreq){
pq.push(item.first);
}
for(int i = 0; i < k; i++){
result.push_back(pq.top());
pq.pop();
}
return result;
}