C ++ declares a priority queue in else / if?

I have two different priority queues that use different comparators. I would like a boolean if / else to determine which pq the variable will be given (eg "pq");

For example, I have

priority_queue<test, vector<test>, CompareTest1> pq;

      

How would I put this in an if / else so that if a boolean is flagged pq will be set as ...

priority_queue<test, vector<test>, CompareTest2> pq;

      

using a different comparator. Thank.

+1


source to share


2 answers


This is not possible, because if you specify a different template, both templates are different types. My suggestion was to pass in a comparison functor that has a boolean value that is determined by what should have been in that if statement. For example:



struct compare {
    bool b;   
    //set this instead of the if statement and allow the function to do something different
    operator () (const test & lhs, const test & rhs) {
    }
};

      

+2


source


The structure of the priority queue doesn't allow you to search for what you are looking for - the items are ordered in the priority queue according to their comparator. Creating a priority queue using another comparator is the same as creating a new priority queue.



0


source







All Articles