栈和队列(忘了拿出来看看)

栈和队列
1、头文件:
#include//栈
#include//队列
#include双向队列
using namespace std;
2、定义:
stack x;
queue y;
deque z;
3、操作:
栈:
x.empty()//如果栈为空返回true,否则返回false
x.size()//返回栈中元素的个数
x.pop()//删除栈顶元素但不返回其值
x.top()//返回栈顶的元素,但不删除该元素
x.push(X)//在栈顶压入新元素 ,参数X为要压入的元素
队列:
y.empty()// 如果队列为空返回true,否则返回false
y.size() // 返回队列中元素的个数
y.pop() //删除队列首元素但不返回其值
y.front() // 返回队首元素的值,但不删除该元素
y.push(X) //在队尾压入新元素 ,X为要压入的元素
y.back() //返回队列尾元素的值,但不删除该元素
双向队列:
z.pop_front() //删除头部的元素
z.pop_back() //删除尾部的元素
z.size() //返回双向队列中元素的个数
z.front() //返回双向队列头部的元素,但不删除该元素
z.back() //返回双向队列尾部的元素,但不删除该元素
z.empty() //如果双向队列为空返回true
优先队列:
默认为从大到小输出

priority_queue <int> q;

greater和less优先队列:

priority_queue <int,vector<int>,less<int> > p;//从大到小
priority_queue <int,vector<int>,greater<int> > q;//从小到大

对于结构体使用优先队列

#include <iostream>
#include <queue>
using namespace std;
struct node {
	int a, b;
	bool operator< (const node& x) const {
		if (this->a != x.a)
			return this->a < x.a;
		//堆顶优先a最大的,在a相等的情况下,堆顶优先b最小的
		else
			return this->b > x.b;
	}
}now1;
int main() {
	priority_queue<node>que;
	while (cin >> now1.a >> now1.b) {
		que.push(now1);
	}
	while (!que.empty()) {
		now1 = que.top();
		que.pop();
		cout << now1.a << " " << now1.b << endl;
	}
	return 0;
}

posted on 2022-06-27 08:29  wxz0v0  阅读(11)  评论(0)    收藏  举报

导航