模板——STL队列

C++ STL queue 容器优先队列&&队列

队列

 1 #include<queue>
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6     queue<string> x;
 7     x.empty();
 8     for(int i=1;i<=10;i++)
 9     {
10       string y;
11       cin>>y;
12       x.push(y);
13     }
23     return 0;
24 }

优先队列(运算符重载)队首为最小元素:

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int tmp[100];
 7 struct cmp1
 8 {
 9     bool operator()(int x,int y)
10     {
11         return x>y;
12     }
13 };
14 priority_queue<int,vector<int>,cmp1>q2;
15 
16 int main()
17 {
18     int n,ans=0;
19 }

优先队列,队首为最大元素:

priority_queue<int>q2;

说明:

包含:

 #include<vector>//优先队列
 #include<queue>

基本操作:

x.push(y);//将y元素加入x队列
x.size();//询问x队列长度
x.front()//访问队列x中最先加入的元素
x.back()//访问队列x中最后加入的元素
x.pop();//弹出队列x中最先加入的元素
x.empty();//判断队列x是否为空,为空返回1,否则返回0

队列插入开销很小,但只能访问和弹出队首元素,无法做到修改队列中的元素值。

STl队列在某些方面还是很好用的,减小时空复杂度,操作的函数名也是很科学的,不会出现看不懂的情况。

主要是方便可以偷点小懒。(划去)

手动滑稽^_^

———————————————————————————————— 执笔饰年华,一笑叹天涯。逢春即润物,生当得潇洒。
原文地址:https://www.cnblogs.com/SuperGoodGame/p/9044121.html