priority_queue_top

priority_queue_top (std::priority_queue::top — top 원소 참조)

std::priority_queue의 top 원소에 대한 참조를 반환하는 멤버 함수예요. 우선순위가 가장 높은(기본적으로 가장 큰) 원소예요.

출처: cppreference

본문

시그니처는 다음과 같아요.

const_reference top() const;

우선순위 큐의 top 원소에 대한 참조를 반환해요. pop()을 호출하면 제거될 원소예요. 기본 비교(std::less)를 쓰면 가장 큰 원소예요. 사용자 정의 Compare를 쓰면 그 기준으로 우선순위가 가장 높은 원소예요.

반환값

top 원소에 대한 참조.

복잡도

상수(constant)예요.

예제

#include <iostream>
#include <queue>
int main()
{
    std::priority_queue<int> pq;
    pq.push(2); pq.push(5); pq.push(1);
    std::cout << pq.top() << '\n';   // 5
    // 산술 허용: top()은 const_reference 반환
}

함께 보기

  • push: 우선순위 큐에 원소를 삽입해요
  • pop: top 원소를 제거해요

더 알아보기 (Learn more)

cppreference