Ankur S
Ankur S

Reputation: 588

Why does queue have front but priority queue has top in stl?

Both are container adaptors and both are defined in the header <queue> still they both have different interfaces to access the 'first' element. I can understand the lack of a back due to the constraints of the data structure but naming front differently confuses me.

Upvotes: 4

Views: 1833

Answers (2)

Loki Astari
Loki Astari

Reputation: 264461

Yes that's the point.

These are two different adapters. They "adapt" an underlying container to have a specific interface. These two classes "define" the interface that can then used by algorithms.

Why that terminology?

  • Well a queue has a front and back.
  • There is not requirement for a priority to queue to get the least prioritized element; it just has the most prioritized element. So I suppose you could have just a front but probably to differentiate it from queue they choose top. It probably has something to do with the implementation (as it is almost definitely a heap underneath the covers which just has a top).

Upvotes: 2

Brian Bi
Brian Bi

Reputation: 119229

The priority_queue container adaptor is a convenience wrapper for the standard library's heap algorithms, using a sequence container as a classic binary heap. The name top likely reflects this association; we speak of the "top" of a heap, since we visualize it as a heap-ordered binary tree, with the element of greatest priority at the root (top).

Upvotes: 2

Related Questions