다음을 통해 공유


Visual C++에서 priority_queue::(push, pop, empty, top) STL 함수 사용

이 문서는 Visual C++에서 , priority_queue::pop, priority_queue::toppriority_queue::emptypriority_queue::size STL 함수를 사용하는 priority_queue::push방법을 해결하는 데 도움이 됩니다.

원래 제품 버전: Visual C++
원래 KB 번호: 157623

요약

아래 샘플 코드는 Visual C++에서 , priority_queue::pop, priority_queue::emptypriority_queue::toppriority_queue::size STL 함수를 사용하는 priority_queue::push방법을 보여 줍니다.

priority_queue 어댑터는 에서 지원하는 컨테이너 형식으로 정의된 형식의 개체를 보유합니다priority_queue. 지원되는 두 컨테이너는 다음과 deque같습니다vector. 개체는 에 의해 push() 삽입되고 제거됩니다 pop(). top() 에서 맨 위 항목을 반환합니다 priority_queue.

어댑터는 반복 priority_queue 을 지원하지 않으므로 연결된 반복기가 없습니다.

Priority_queue 를 사용하면 더 작음, 더 큰 등과 같이 연결된 비교자 함수에 의해 결정되는 항목의 정렬된 컬렉션을 유지할 수 있습니다. 따라서 상위 항목은 선택한 함수에 따라 가장 낮거나 가장 높은 선택 후보가 됩니다.

필수 헤더

<queue>

프로토타입

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

참고 항목

프로토타입의 클래스/매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 가독성을 향상시키기 위해 Sme가 수정되었습니다.

샘플 코드

샘플에서는 구현 사용 dequevector 컨테이너를 보여 priority_queue줍니다.

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// <filename> : priority_queue.cpp
// Functions:
// priority_queue::push(), priority_queue::pop(),
// priority_queue::empty(), priority_queue::top(), queue::size()
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;

#if _MSC_VER > 1020 // if VC++ version is > 4.2
    using namespace std; // std c++ libs implemented in std
#endif

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

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

void main(void)
{
    int 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();
    }
}

프로그램 출력:

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