1

I had to pass an unordered map to the comparator function of a priority queue and using the link Passing a parameter to a comparison function? I decided to do it as follows:

 priority_queue < int, std::vector<int>, compare(freq) > pq;
 struct compare
    {
        compare( std::unordered_map<int,int>& freq1 )
        {
            freq = freq1;
        }

        bool operator()( int& el1, int& el2 ){
            return freq[el1] < freq[el2];
        }    
        std::unordered_map<int,int> freq;
    };

However I'm getting the error:

Template argument for template type parameter must be a type

What am I doing wrong?

nikgpuec
  • 45
  • 1
  • 7
  • 2
    Does this answer your question? [How can I create my own comparator for a map?](https://stackoverflow.com/questions/5733254/how-can-i-create-my-own-comparator-for-a-map) - it's the same for an `unordered_map` – Lukas-T Jun 09 '20 at 15:46
  • @churill: not sure if I understand. Can you please elaborate ? – nikgpuec Jun 09 '20 at 16:04

1 Answers1

1

As the error message said, compare(freq) is not a type, it can't be specified as the type template argument.

You should specify compare(freq) as the argument of the constructor of priority_queue, and specify compare as the type template argument.

priority_queue < int, std::vector<int>, compare> pq{compare(freq)};
//                                      ^^^^^^^    ^^^^^^^^^^^^^^^ 
songyuanyao
  • 169,198
  • 16
  • 310
  • 405