Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Could someone explain what this variable definition (in C++ solution) does: pri...

Athanasios Petsas

Feb 16, 2022

Could someone explain what this variable definition (in C++ solution) does: priority_queue, greater> minHeap; ? the greater is a functor, right? To me it seems like applying the greater functor for all the elements of a priority queue. Is it like having a maxHeap and through this greater functor we are turning it into a minHeap?

0

0

Comments
Comments
Design Gurus
Design Gurus4 years ago

yes, std::greater would cause the smallest element to appear as the top().

https://en.cppreference.com/w/cpp/container/priority_queue

A
Athanasios Petsas4 years ago

Thanks! It makes sense!

A
Athanasios Petsas4 years ago

will this introduce some overhead comparing to maxHeap, since it manipulates the vector? Does it do it every time?

Design Gurus
Design Gurus4 years ago

Each priority queue has a comparing function. The default implementation is for max heap. Please see the documentation for details.

A
Athanasios Petsas4 years ago

I checked it out, thanks!

On this page