public member function
<queue>

std::priority_queue::top

const value_type& top() const;
const_reference top() const;
Access top element
Returns a constant reference to the top element in the priority_queue.

The top element is the element that compares higher in the priority_queue, and the next that is removed from the container when priority_queue::pop is called.

This member function effectively calls member front of the underlying container object.

Parameters

none

Return value

A reference to the top element in the priority_queue.

Member type value_type is the type of the elements in the container (defined as an alias of the first class template parameter, T).
Member type const_reference is an alias of the underlying container's type with the same name.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// priority_queue::top
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> mypq;

  mypq.push(10);
  mypq.push(20);
  mypq.push(15);

  std::cout << "mypq.top() is now " << mypq.top() << '\n';

  return 0;
}


Output:
mypq.top() is now 20

Complexity

Constant (calling front on the underlying container).

Data races

The container is accessed.
The constant reference returned can be used to directly access the next element.

Exception safety

Provides the same level of guarantees as the operation performed on the container (no-throw guarantee for standard non-empty containers).

See also