C++におけるSTL-キューと優先キュー

1511 ワード

キュー:
#include
#include
using namespace std;

int main()
{
	queueq;
	q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
	while(!q.empty())
	{
		printf("%d ",q.front());//      front
		q.pop();
	}
	return 0;	
}

出力結果は次のとおりです.
10 5 12 14 6
優先キュー:
#include
#include
using namespace std;

int main()
{
	priority_queueq;
	q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
	while(!q.empty())
	{
		printf("%d ",q.top());//      top
		q.pop();
	}
	return 0;	
}

出力結果:
14 12 10 8 6
優先キュー構造体:
#include
#include
using namespace std;
struct data{
	int a;
	int b;
	friend bool operatorq;
	data p;
	int n=3;
	while(n--)
	{
		scanf("%d%d",&p.a,&p.b);
		q.push(p);
	}
	while(!q.empty())
	{
		p=q.top();
		printf("%d %d
",p.a,p.b); q.pop(); } return 0; }

入力:
1 2
4 3
3 4
出力:
3 4
4 3
1 2
優先キュー構造体、外での再ロード
#include
#include
using namespace std;
#include
struct data{
	int a;
	int b;
}A;
bool operatorq;
	for(i=0;i<3;i++)
	{
		scanf("%d",&A.a);
		q.push(A);
	}
	while(!q.empty())
	{
		A=q.top();
		printf("%d ",A.a);
		q.pop();
	}
	return 0;
}
bool operatory.a;
}

入力:
1 3 2
出力:
1 2 3