STL之stack,queue,priority_queue

版权声明:原创文章,但可以转载哦~ https://blog.csdn.net/Jarvenman/article/details/88735064

优先队列的某些部分,参考了另一篇博文,那篇博文里还有一些关于优先队列的其他东西,但是我觉得用处不大,所以没有写下来,读者有兴趣可以看一下
https://www.cnblogs.com/xzxl/p/7266404.html

一、stack —— 栈,后进先出
常用函数:

	#include<stack>			//头文件
	stack<int> q;			//定义q
	q.top();				//获取栈顶元素(不删除)
	q.pop();				//删除栈顶元素
	q.push(x);				//x入栈
	q.empty();				//判断栈空否		while(!q.empty()){...}
	q.size();				//返回栈的大小

代码示例:

#include<iostream>
#include<stack>             //stack头文件
using namespace std;
int main()
{
    int x;
    stack<int> q;           //初始化栈
    while(cin>>x)
    {
        q.push(x);          //入栈
    }
    cout<<"The size of the stack is "<<q.size()<<endl;      //输出栈的大小
    while(!q.empty())
    {
        cout<<q.top()<<" ";     //输出栈顶元素
        q.pop();                //栈顶元素出栈            应该与入栈顺序相反
    }
}

输出样例:(^z是终止符)
在这里插入图片描述
二、queue —— 队列,先进先出
常用函数:

	#include<queue>			//头文件
	queue<int> q;			//定义q
	q.front();				//获取队首元素(不删除)
	q.pop();				//删除队首元素
	q.push(x);				//x入队
	q.empty();				//判断队空否		while(!q.empty()){...}
	q.size();				//判断队的大小

代码示例:

#include<iostream>
#include<queue>             //queue头文件
using namespace std;
int main()
{
    int x;
    queue<int> q;           //初始化队列
    while(cin>>x)
    {
        q.push(x);          //入队
    }
    cout<<"The size of the queue is "<<q.size()<<endl;      //输出队列的大小
    while(!q.empty())
    {
        cout<<q.front()<<" ";     //输出队列元素
        q.pop();                //元素出队            应该与入队顺序相同
    }
}


输出示例:
在这里插入图片描述
三、优先队列:
优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素。但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队。这点类似于给队列里的元素进行了由大到小的顺序排序。元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。(摘自参考博客)

常用函数:

	#include<queue>							//头文件,和queue一致
	priority_queue<int> q;          							 //定义优先队列,元素从大到小排序
    priority_queue<int, vector<int>, greater<int> > p;           //定义优先队列,元素从小到大排序
	q.top();				//获取队优先级最高的元素(不删除),注意哦,这里和queue不一样,反而和stack是一样的
	q.pop();				//删除优先级最高的元素
	q.push(x);				//x入队
	q.empty();				//判断队空否		while(!q.empty()){...}
	q.size();				//返回队的大小

代码示例:

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main()
{
    priority_queue<int> q;          //定义优先队列,元素从大到小排序
    priority_queue<int, vector<int>, greater<int> > p;           //定义优先队列,元素从小到大排序
    int x;
    while(cin>>x)
    {
        q.push(x);
        p.push(x);
    }
    while(!q.empty())
    {
        cout<<q.top()<<" ";
        q.pop();
    }
    cout<<endl;
    while(!p.empty())
    {
        cout<<p.top()<<" ";
        p.pop();
    }
}

输出样例:
在这里插入图片描述
这里还有一种情况,是如果优先队列里装的是我们自己定义的结构体呢?如何自定义优先级?
看如下代码:

struct Node
	{
		int x, y;
		friend bool operator < (Node a, Node b)                 //friend是C++中的关键字,意为“友元”
		{
			return a.x > b.x;           //x小的优先级高一些
		}
	};

整段代码如下:

#include<iostream>
#include<queue>
using namespace std;

struct Node
{
    int x, y;
    friend bool operator < (Node a, Node b)                 //friend是C++中的关键字,意为“友元”
    {
        return a.x > b.x;           //x小的优先级高一些
    }
};
int main()
{
    priority_queue<Node> p;
    int nx, ny;
    while(cin>>nx>>ny)
    {
        Node n;
        n.x = nx;
        n.y = ny;
        p.push(n);
    }
    while(!p.empty())
    {
        cout<<p.top().x<<" "<<p.top().y;
        p.pop();
        cout<<endl;
    }

}

输出示例如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Jarvenman/article/details/88735064