C++标准模版库STL容器适配器——queue、priority_queue和stack容器

容器适配器

容器适配器是用基本容器实现的一些新容器,这些容器可以用于描述更高级的数据结构。本质上,适配器是使一事物的行为类似于另一类事物的行为的一种机制。容器适配器让一种已存在的容器类型采用另一种不同的抽象类型的工作方式实现。

容器适配器有三种:stack、queue和priority_queue。stack可以与数据结构中的栈对应,它具有先进后出的特性,而queue则可以理解为队列,它具有先进先出的特性,priority_queue则是带优先级的队列,其元素可以按照某种优先级顺序进行删除。
在这里插入图片描述

queue的常见用法详解

在这里插入图片描述

queue容器内元素的访问front()、back()

front()和back()可分别获得队首元素和队尾元素

示例代码

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	queue<int> q;
	for(int i = 1; i <= 5; i++)
	{
    
    
		q.push(i);
	}
	printf("%d %d\n", q.front(), q.back());
	return 0;
}

运行结果:
在这里插入图片描述

pop()

pop()令队首元素出队

示例代码

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	queue<int> q;
	for(int i = 1; i <= 5; i++)
	{
    
    
		q.push(i);
	}
	for(int i = 1; i <= 3; i++)
	{
    
    
		q.pop();
	} 
	printf("%d\n", q.front());
	return 0;
}

运行结果:
在这里插入图片描述

empty()

empty()检验queue是否为空,返回true说明队列为空,返回false为非空

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	queue<int> q;
	if(q.empty() == true)
	{
    
    
		printf("Empty!\n");
	}
	else
	{
    
    
		printf("Not Empty!\n");
	}
	
	q.push(1);
	if(q.empty() == true)
	{
    
    
		printf("Empty!\n");
	}
	else
	{
    
    
		printf("Not Empty!\n");
	}
	printf("%d\n", q.front());
	return 0;
}

运行结果:
在这里插入图片描述

size()

size()返回queue内元素的个数,时间复杂度为O(1)

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	queue<int> q;
	for(int i = 1; i <= 5; i++)
	{
    
    
		q.push(i);
	}
	printf("%d\n", q.size());
	return 0;
}

运行结果:
在这里插入图片描述
Attention!!!
在使用front()和pop()函数前,必须用empty()判断队列是否为空

综合示例:

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

int main()
{
    
    
	queue<int> q;
	cout << "将数据{25, 17, 6, 100, 59}插入队列" << endl;
	q.push(25);
	q.push(17);
	q.push(6);
	q.push(100);
	q.push(59);
	
	cout << "queue大小为:" << q.size() << endl;
	cout << "队列头:" << q.front() << endl;
	cout << "队列尾:" << q.back() << endl;
	while(q.size() != 0)
	{
    
    
		cout << "删除队列头" << q.front() << endl;
		q.pop();
	}
	if(q.empty())
	{
    
    
		cout << "队列为空" << endl;
	}
	return 0;
} 

运行结果:
在这里插入图片描述

priority_queue的常见用法详解

priority_queue又称优先队列,与queue的不同之处在于,包含最大值的元素位于队首,且只能在队首执行操作。
在这里插入图片描述

priority_queue容器内元素的访问

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	priority_queue<int> q;
	q.push(3);
	q.push(4);
	q.push(1);
	printf("%d\n", q.top());
	return 0;
}

运行结果:
在这里插入图片描述

priority_queue常用函数实例解析

push()

push(x)令元素x入队

top()

获取队首元素(堆顶元素)

pop()

令队首元素(堆顶元素)出队

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	priority_queue<int> q;
	q.push(3);
	q.push(4);
	q.push(1);
	printf("%d\n", q.top());
	q.pop();
	printf("%d\n", q.top());
	return 0;
}

运行结果:
在这里插入图片描述

empty()

判断队列是否为空,true表示空

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	priority_queue<int> q;
	if(q.empty() == true)
	{
    
    
		printf("Empty!\n");
	}
	else
	{
    
    
		printf("Not Empty!\n");
	}
	
	q.push(1);
	if(q.empty() == true)
	{
    
    
		printf("Empty!\n");
	}
	else
	{
    
    
		printf("Not Empty!\n");
	}
	return 0;
}

运行结果:
在这里插入图片描述

size()

返回队列内元素的个数

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	priority_queue<int> q;
	q.push(3);
	q.push(4);
	q.push(1);
	printf("%d\n", q.size());
	return 0;
}

运行结果:
在这里插入图片描述

priority_queue内元素优先级的设置

基本数据类型的优先级设置

基本数据类型是指int、double、char等,默认的优先级设置是数字越大的优先级越高

priority_queue< int > q;
等价于
priority_queue< int, vector< int >, less< int > > q;
vecotr< int >是来承载底层数据结构堆(heap)的容器,less< int >表示数字越大优先级越大,而greater< int > 表示数字越小优先级越大

示例代码:

#include<stdio.h>
#include<queue>
using namespace std;

int main()
{
    
    
	priority_queue<int, vector<int>, greater<int> > q;
	q.push(3);
	q.push(4);
	q.push(1);
	printf("%d\n", q.top());
	return 0;
}

运行结果:
在这里插入图片描述

结构体的优先级设置

overload小于号<,价格高的水果优先级高

示例代码:

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

struct fruit{
    
    
	string name;
	int price;
	friend bool operator < (fruit f1, fruit f2){
    
    
		return f1.price < f2.price;
	} 
}f1, f2, f3; 

int main()
{
    
    
	priority_queue<fruit> q;
	f1.name = "桃子";
	f1.price = 3;
	f2.name = "梨子";
	f2.price = 4;
	f3.name = "苹果";
	f3.price = 1;
	q.push(f1);
	q.push(f2);
	q.push(f3);
	cout << q.top().name << " " << q.top().price << endl;
	
	return 0; 
}

运行结果:
在这里插入图片描述
另一种定义方式,将重载函数写在结构体外面

示例代码:

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

struct fruit{
    
    
	string name;
	int price;
}f1, f2, f3;

struct cmp{
    
    
	bool operator () (fruit f1, fruit f2){
    
    
		return f1.price > f2.price;
	}
};

int main()
{
    
    
	priority_queue<fruit, vector<fruit>, cmp> q;
	f1.name = "桃子";
	f1.price = 3;
	f2.name = "梨子";
	f2.price = 4;
	f3.name = "苹果";
	f3.price = 1;
	
	q.push(f1);
	q.push(f2);
	q.push(f3);
	cout << q.top().name << " " << q.top().price << endl;
	return 0; 
}

运行结果:
在这里插入图片描述
Attention!!!
在使用top()前要用empty()判断优先队列是否为空

stack的常见用法详解

stack(栈)允许在顶部插入和删除元素,但不能访问中间的元素。因此stack的行为就像叠盘子一样。是一种后进先出的数据结构

stack容器内元素的访问

示例代码:

#include<stdio.h>
#include<stack>
using namespace std;

int main()
{
    
    
	stack<int> st;
	for(int i = 1; i <= 5; i++)
	{
    
    
		st.push(i);
	}
	
	printf("%d\n", st.top());
	return 0;
}

运行结果:
在这里插入图片描述

stack常用函数实例解析

push()

push(x)令元素x入栈

top()

获取栈顶元素

pop()

弹出栈顶元素

示例代码:

#include<stdio.h>
#include<stack>
using namespace std;

int main()
{
    
    
	stack<int> st;
	for(int i = 1; i <= 5; i++)
	{
    
    
		st.push(i);
	}
	
	for(int i = 1; i <= 3; i++)
	{
    
    
		st.pop();
	}
	printf("%d\n", st.top());
	return 0;
}

运行结果:
在这里插入图片描述

empty()

判断stack内是否为空,true表示空

size()

返回stack内元素的个数

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

猜你喜欢

转载自blog.csdn.net/qq_44631615/article/details/112302962