I have stumbled upon some weird use of priority_queue, I would love to obtain some proper explanation of why on earth it's plausibile/valid to use something like this in priority_queue declaration:
typedef priority_queue<RandomContainer, **vector<RandomContainer>**, FunctorName> NewQueueName; Let's say we've got some struct called SPerson:
struct SPerson { int age; string name; string surname; }; and some functor which will be helpful to sort all elements of queue accordingly to our likeing:
struct TheWayILike { bool operator()(const SPerson &name1, const SPerson &name2) { if(name1.name > name2.name) return true; if(name1.name < name2.name) return false; return false; } }; Now we can declare our priority_queue which will be based upon elements from the struct and which will be ordered by functor called TheWayILike.
priority_queue<SPerson, TheWayILike> or shorter way by using typedef and single name like so:
typedef priority_queue<SPerson, TheyWayILike> newNameForPQ; but somehow it's wrong and I have to add following line: vector
Question:
Why on earth do I have to squize my personally customized data types into vector ?
Why it must be a vector and why should I use it anyway ?
Why do I need to fill my data into vector ? I haven't read about it in official priority_queue documentation, so I would love to obtain some easy to understand explanation for rookie programmer.
Cheers!
Containeris only a template parameter. It tellspriority_queuewhat to use internally, you do not have to provide an instance of this container! In other words, create your queue like so:priority_queue<SPerson, vector<SPerson>, TheWayILike> myPQand push your SPerson elements in it:myPQ.push(a); myPQ.push(b); ...