다음을 통해 공유


priority_queue Functions

사용 하는 방법을 보여 줍니다 있는 priority_queue::push, priority_queue::pop, priority_queue::empty,priority_queue::top, 및 priority_queue::size Visual C++에서 표준 템플릿 라이브러리 (STL) 함수입니다. 

priority_queue::push( ); 
priority_queue::pop( ); 
priority_queue::empty( ); 
priority_queue::top( ); 
priority_queue::size( );

설명

[!참고]

프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

샘플 프로그램을 priority_queue 벡터 및 있지 않은 deque 컨테이너를 사용 하 여 구현 합니다.

예제

// priority_queue.cpp
// compile with: /EHsc
//
// Functions:
//    priority_queue::push(), priority_queue::pop(),
//    priority_queue::empty(), priority_queue::top(), queue::size()

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>

using namespace std ;

// Using priority_queue with deque
// Use of function less sorts the items in ascending order
typedef deque<int> INTDQU;
typedef priority_queue<int, INTDQU, less<int> > INTPRQUE;

// Using priority_queue with vector
// Use of function greater sorts the items in descending order
typedef vector<char> CHVECTOR;
typedef priority_queue<char, CHVECTOR, greater<char> > CHPRQUE;

int main(void)
{
    size_t size_q;
    INTPRQUE   q;
    CHPRQUE    p;

    // Insert items in the priority_queue(uses deque)
    q.push(42);
    q.push(100);
    q.push(49);
    q.push(201);

    // Output the item at the top using top()
    cout << q.top() << endl;

    // Output the size of priority_queue
    size_q = q.size();
    cout << "size of q is:" << size_q << endl;

   // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!q.empty())
    {
        cout << q.top() << endl;
        q.pop();

    }

// Insert items in the priority_queue(uses vector)
    p.push('c');
    p.push('a');
    p.push('d');
    p.push('m');
    p.push('h');

    // Output the item at the top using top()
    cout << p.top() << endl;

    // Output the size of priority_queue
    size_q = p.size();
    cout << "size of p is:" << size_q << endl;

    // Output items in priority_queue using top()
    // and use pop() to get to next item until
    // priority_queue is empty
    while (!p.empty())
    {
        cout << p.top() << endl;
        p.pop();

    }
}

Output

201
size of q is:4
201
100
49
42
a
size of p is:5
a
c
d
h
m

요구 사항

헤더: <queue>

참고 항목

개념

표준 템플릿 라이브러리 샘플