우선 대기 열의 용법
2027 단어 데이터 구조
1. 우선 대기 열 을 직접 정의 하면 시스템 의 기본 값 은 내림차 우선 대기 열 입 니 다.
priority_queue pq;
2. 시스템 의 기본 적 인 기 존 구조 greater 를 통 해 오름차 의 우선 대기 열 을 정의 할 수 있 습 니 다.greater 와 유사 한 것 은 less 이다.
#include
#include
#include
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
priority_queue,greater > pq;
for(int i=10;i>0;i--)
{
pq.push(rand());
}
while(!pq.empty())
{
cout<
3. 대기 열 요소 가 특정한 구조 체 라면 다시 불 러 올 수 있 습 니 다.
#include
#include
#include
#include
#include
using namespace std;
struct node
{
int data;
bool operator a.data; // , pq;
for(int i=10;i>0;i--)
{
node t;
t.data=rand();
pq.push(t);
}
while(!pq.empty())
{
cout<
4. 구조 체 정의 우선 순 위 는 const 를 추가 하지 않 아 도 되 지만 친구 로 정의 해 야 합 니 다.
struct node
{
int data;
friend bool operator b.data; // ,
5. 구조 체 에 대한 정 의 는 우선 순위 정 의 를 사용 할 때 쌓 인 특성 에 따라 다음 과 같이 우선 순 위 를 정의 할 수 있다.
#include
#include
#include
#include
#include
using namespace std;
struct node
{
int data;
};
struct nodeCmp
{
bool operator()(const node &a,const node &b)
{
return a.data>b.data; // ,,nodeCmp> pq; //
for(int i=10;i>0;i--)
{
node t;
t.data=rand();
pq.push(t);
}
while(!pq.empty())
{
cout<