priority_queue 클래스

항상 가장 크거나 우선 순위가 가장 높은 일부 기본 컨테이너 형식의 최상위 요소에 대한 액세스를 제한하는 기능 제한을 제공하는 템플릿 컨테이너 어댑터 클래스입니다. 새 요소를 추가할 priority_queue 수 있으며 해당 요소의 priority_queue 최상위 요소를 검사하거나 제거할 수 있습니다.

구문

template <class Type, class Container= vector <Type>, class Compare= less <typename Container ::value_type>>
class priority_queue

매개 변수

Type
priority_queue에 저장되는 요소 데이터 형식입니다.

Container
를 구현하는 데 사용되는 기본 컨테이너의 형식입니다 priority_queue.

Compare
두 요소 값을 정렬 키로 비교하여 상대적 순서를 결정할 수 있는 priority_queue함수 개체를 제공하는 형식입니다. 이 인수는 선택 사항이며 기본값은 이진 조건자 less<typename Container::value_type>입니다.

설명

큐 개체의 첫 번째 템플릿 매개 변수에 규정된 클래스 Type 의 요소는 동의어 value_type 이며 두 번째 템플릿 매개 변수로 규정된 기본 컨테이너 클래스 Container 의 요소 형식과 일치해야 합니다. Type 해당 형식의 개체를 복사하고 해당 형식의 변수에 값을 할당할 수 있도록 할당할 수 있어야 합니다.

priority_queue 클래스Traits의 저장된 함수 개체를 호출하여 제어하는 시퀀스를 정렬합니다. 일반적으로 요소는 이 순서를 설정하는 데 비해 작을 필요가 있습니다. 따라서 두 요소가 동일하거나(둘 다 다른 요소보다 작지 않음) 다른 요소보다 작다는 것을 확인할 수 있습니다. 그러면 동일하지 않은 요소 사이에 정렬이 수행됩니다. 기술적으로 설명하면, 비교 함수는 표준 함수의 의미에서 엄밀히 약한 정렬을 수행하는 이진 조건자입니다.

포함deque클래스 및 기본 vector 클래스 또는 , pop_backpush_back및 임의 front액세스 반복기의 작업을 지원하는 다른 시퀀스 컨테이너에 적합한 기본 컨테이너 클래스 priority_queue 입니다. 기본 컨테이너 클래스는 제한된 시퀀스 컨테이너 멤버 함수 집합만 공용 인터페이스로 표시하는 컨테이너 어댑터 내에서 캡슐화되어 있습니다.

priority_queue에 요소를 추가하고 priority_deque에서 요소를 제거하는 과정은 복잡한 로그 작업입니다. priority_queue에서 요소에 액세스하는 과정은 복잡한 상수 작업입니다.

C++ 표준 라이브러리에서 정의한 세 가지 유형의 컨테이너 어댑터가 있습니다. stackqueuepriority_queue 각 어댑터는 일부 기본 컨테이너 클래스의 기능을 제한하여 표준 데이터 구조에 대해 정확하게 제어되는 인터페이스를 제공합니다.

  • 클래스stack LIFO(Last-in, first-out) 데이터 구조를 지원합니다. 쌓여 있는 접시 더미의 예로 이해할 수 있습니다. 요소(접시)는 기본 컨테이너의 끝에 있는 마지막 요소인 스택의 맨 위에서만 삽입하거나 검사하거나 제거할 수 있습니다. 상위 요소에만 액세스하는 제한은 클래스를 사용하는 stack 이유입니다.

  • 클래스queue FIFO(선점) 데이터 구조를 지원합니다. 은행 창구 직원을 만나려고 줄 서 있는 사람들의 예로 이해할 수 있습니다. 요소(사람)는 줄의 뒤에 추가될 수 있고 줄의 앞에서 제거됩니다. 줄의 앞과 뒤는 모두 검사할 수 있습니다. 이러한 방식으로 전면 및 후면 요소에만 액세스하는 제한은 클래스를 사용하는 queue 이유입니다.

  • 클래스는 priority_queue 가장 큰 요소가 항상 맨 위 위치에 있도록 해당 요소를 정렬합니다. 이 클래스는 요소의 삽입과 최상위 요소의 검사 및 제거를 지원합니다. 명심해야 할 좋은 아날로그는 나이, 높이 또는 기타 기준에 따라 정렬된 위치에 줄지어 서 있는 사람들이 될 것입니다.

생성자

생성자 Description
priority_queue 비어 있거나 기본 컨테이너 개체 또는 다른 priority_queue 범위의 복사본인 priority_queue를 생성합니다.

Typedef

형식 이름 설명
container_type priority_queue에서 조정할 기본 컨테이너를 제공하는 형식입니다.
size_type priority_queue에서 요소 수를 표현할 수 있는 부호 없는 정수 형식입니다.
value_type priority_queue에 있는 요소로 저장된 개체의 형식을 나타내는 형식입니다.

멤버 함수

멤버 함수 설명
empty priority_queue이 비어 있는지를 테스트합니다.
pop priority_queue의 가장 큰 요소를 최상위 위치에서 제거합니다.
push 에서 요소의 우선 순위에 따라 우선 순위 큐에 요소를 operator<추가합니다.
size priority_queue에 있는 요소 수를 반환합니다.
top priority_queue의 최상위 위치에 있는 가장 큰 요소에 대한 const 참조를 반환합니다.

요구 사항

헤더<queue>:

네임스페이스:std

priority_queue::container_type

조정할 기본 컨테이너를 제공하는 형식입니다.

typedef Container container_type;

설명

이 형식은 템플릿 매개 변수 Container의 동의어입니다. C++ 표준 라이브러리 시퀀스 컨테이너 클래스 deque 및 기본 클래스 vector 는 개체의 기본 컨테이너 priority_queue 로 사용할 요구 사항을 충족합니다. 이 요구 사항을 충족하는 사용자 정의 형식도 사용할 수 있습니다.

자세한 Container내용은 수업 항목의 설명 섹션을 priority_queue 참조하세요 .

예시

선언하고 사용하는 container_type방법에 대한 priority_queue 예제는 예제를 참조하세요.

priority_queue::empty

priority_queue가 비어 있는지 여부를 테스트합니다.

bool empty() const;

Return Value

true비어 priority_queue 있으면 이고, false 비어 있지 않으면 priority_queue

예시

// pqueue_empty.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares priority_queues with default deque base container
   priority_queue <int> q1, s2;

   q1.push( 1 );

   if ( q1.empty( ) )
      cout << "The priority_queue q1 is empty." << endl;
   else
      cout << "The priority_queue q1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The priority_queue s2 is empty." << endl;
   else
      cout << "The priority_queue s2 is not empty." << endl;
}
The priority_queue q1 is not empty.
The priority_queue s2 is empty.

priority_queue::pop

priority_queue의 가장 큰 요소를 최상위 위치에서 제거합니다.

void pop();

설명

priority_queue 멤버 함수를 적용하려면 비어 있지 않아야 합니다. 위쪽 priority_queue 은 항상 컨테이너에서 가장 큰 요소가 차지합니다.

예시

// pqueue_pop.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue <int> q1, s2;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   priority_queue <int>::size_type i, iii;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;

   q1.pop( );

   iii = q1.size( );
   cout << "After a pop, the priority_queue length is "
        << iii << "." << endl;

   const int& iv = q1.top( );
   cout << "After a pop, the element at the top of the "
        << "priority_queue is " << iv << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.
After a pop, the priority_queue length is 2.
After a pop, the element at the top of the priority_queue is 20.

priority_queue::priority_queue

priority_queue 비어 있거나 기본 컨테이너 개체 또는 다른 priority_queue컨테이너 개체의 범위 복사본을 생성합니다.

priority_queue();

explicit priority_queue(const Traits& _comp);

priority_queue(const Traits& _comp, const container_type& _Cont);

priority_queue(const priority_queue& right);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Traits& _comp);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Traits& _comp, const container_type& _Cont);

매개 변수

_comp
기본 컨테이너의 함수를 비교하는 기본값인 요소priority_queue의 순서를 지정하는 데 사용되는 형식 constTraits 의 비교 함수입니다.

_Cont
생성된 priority_queue 기본 컨테이너는 복사본이 됩니다.

right
priority_queue 생성된 집합이 복사본이 될 것입니다.

first
복사할 요소의 범위에서 첫 번째 요소의 위치입니다.

last
복사할 요소의 범위를 벗어나는 첫 번째 요소의 위치입니다.

설명

처음 세 개의 생성자는 각각 빈 이니셜priority_queue을 지정하고, 두 번째 생성자는 요소의 순서를 설정하는 데 사용할 비교 함수(comp)의 형식을 지정하고, 세 번째 생성자는 사용할 (_Cont)를 명시적으로 지정합니다 container_type . explicit 키워드를 사용하는 경우 특정 종류의 자동 형식 변환이 수행되지 않습니다.

네 번째 생성자는 .의 복사본을 priority_queue right지정합니다.

마지막 세 생성자는 일부 컨테이너의 범위를 [first, last) 복사하고 값을 사용하여 클래스 Traits 의 비교 함수 형식을 지정할 때 명시성이 증가하는 값을 초기화 priority_queue 합니다container_type.

예시

// pqueue_ctor.cpp
// compile with: /EHsc
#include <queue>
#include <vector>
#include <deque>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   // The first member function declares priority_queue
   // with a default vector base container
   priority_queue <int> q1;
   cout << "q1 = ( ";
   while ( !q1.empty( ) )
   {
      cout << q1.top( ) << " ";
      q1.pop( );
   }
   cout << ")" << endl;

   // Explicitly declares a priority_queue with nondefault
   // deque base container
   priority_queue <int, deque <int> > q2;
   q2.push( 5 );
   q2.push( 15 );
   q2.push( 10 );
   cout << "q2 = ( ";
   while ( !q2.empty( ) )
   {
      cout << q2.top( ) << " ";
      q2.pop( );
   }
   cout << ")" << endl;

   // This method of printing out the elements of a priority_queue
   // removes the elements from the priority queue, leaving it empty
   cout << "After printing, q2 has " << q2.size( ) << " elements." << endl;

   // The third member function declares a priority_queue
   // with a vector base container and specifies that the comparison
   // function greater is to be used for ordering elements
   priority_queue <int, vector<int>, greater<int> > q3;
   q3.push( 2 );
   q3.push( 1 );
   q3.push( 3 );
   cout << "q3 = ( ";
   while ( !q3.empty( ) )
   {
      cout << q3.top( ) << " ";
      q3.pop( );
   }
   cout << ")" << endl;

   // The fourth member function declares a priority_queue and
   // initializes it with elements copied from another container:
   // first, inserting elements into q1, then copying q1 elements into q4
   q1.push( 100 );
   q1.push( 200 );
   priority_queue <int> q4( q1 );
   cout << "q4 = ( ";
   while ( !q4.empty( ) )
   {
      cout << q4.top( ) << " ";
      q4.pop( );
   }
   cout << ")" << endl;

   // Creates an auxiliary vector object v5 to be used to initialize q5
   vector <int> v5;
   vector <int>::iterator v5_Iter;
   v5.push_back( 10 );
   v5.push_back( 30 );
   v5.push_back( 20 );
   cout << "v5 = ( " ;
   for ( v5_Iter = v5.begin( ) ; v5_Iter != v5.end( ) ; v5_Iter++ )
      cout << *v5_Iter << " ";
   cout << ")" << endl;

   // The fifth member function declares and
   // initializes a priority_queue q5 by copying the
   // range v5[ first,  last) from vector v5
   priority_queue <int> q5( v5.begin( ), v5.begin( ) + 2 );
   cout << "q5 = ( ";
   while ( !q5.empty( ) )
   {
      cout << q5.top( ) << " ";
      q5.pop( );
   }
   cout << ")" << endl;

   // The sixth member function declares a priority_queue q6
   // with a comparison function greater and initializes q6
   // by copying the range v5[ first,  last) from vector v5
   priority_queue <int, vector<int>, greater<int> >
      q6( v5.begin( ), v5.begin( ) + 2 );
   cout << "q6 = ( ";
   while ( !q6.empty( ) )
   {
      cout << q6.top( ) << " ";
      q6.pop( );
   }
   cout << ")" << endl;
}

priority_queue::push

에서 요소의 우선 순위에 따라 우선 순위 큐에 요소를 operator<추가합니다.

void push(const Type& val);

매개 변수

val
의 맨 위에 추가된 요소입니다 priority_queue.

설명

위쪽 priority_queue 은 컨테이너에서 가장 큰 요소가 차지하는 위치입니다.

예시

// pqueue_push.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue<int> q1;

   q1.push( 10 );
   q1.push( 30 );
   q1.push( 20 );

   priority_queue<int>::size_type i;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.

priority_queue::size

priority_queue에 있는 요소 수를 반환합니다.

size_type size() const;

Return Value

의 현재 길이입니다 priority_queue.

예시

// pqueue_size.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue <int> q1, q2;
   priority_queue <int>::size_type i;

   q1.push( 1 );
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   q1.push( 2 );
   i = q1.size( );
   cout << "The priority_queue length is now " << i << "." << endl;
}
The priority_queue length is 1.
The priority_queue length is now 2.

priority_queue::size_type

priority_queue에서 요소 수를 표현할 수 있는 부호 없는 정수 형식입니다.

typedef typename Container::size_type size_type;

설명

형식은 에 의해 priority_queue조정된 기본 컨테이너의 동의어 size_type 입니다.

예시

선언하고 사용하는 size_type방법에 대한 size 예제는 예제를 참조하세요.

priority_queue::top

priority_queue의 최상위 위치에 있는 가장 큰 요소에 대한 const 참조를 반환합니다.

const_reference top() const;

Return Value

함수, 개체에 의해 Traits 결정되는 가장 큰 요소에 대한 참조입니다 priority_queue.

설명

priority_queue 멤버 함수를 적용하려면 비어 있지 않아야 합니다.

예시

// pqueue_top.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue<int> q1;

   q1.push( 10 );
   q1.push( 30 );
   q1.push( 20 );

   priority_queue<int>::size_type i;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.

priority_queue::value_type

priority_queue에 있는 요소로 저장된 개체의 형식을 나타내는 형식입니다.

typedef typename Container::value_type value_type;

설명

형식은 에 의해 priority_queue조정된 기본 컨테이너의 동의어 value_type 입니다.

예시

// pqueue_value_type.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares priority_queues with default deque base container
   priority_queue<int>::value_type AnInt;

   AnInt = 69;
   cout << "The value_type is AnInt = " << AnInt << endl;

   priority_queue<int> q1;
   q1.push( AnInt );
   cout << "The element at the top of the priority_queue is "
        << q1.top( ) << "." << endl;
}
The value_type is AnInt = 69
The element at the top of the priority_queue is 69.

참고 항목

C++ 표준 라이브러리의 스레드 보안
C++ 표준 라이브러리 참조