queue为队列,具有先进先出的特性。使用queue,需要添加#include< queue >和using namespace std;
1、queue的定义:
queue name;//typename可以是任意基本数据类型或容器
queue元素的访问:
由于queue本身就是一种先进先出的限制性数据结构,所以STL只能够提供front()来访问队首元素,用back()来访问队尾元素
#include
#include
using namespace std;
int main(){
queue q;
for(int i = 1 ; i <= 5 ; i++)
q.push(i);
printf("%d",q.front());//1
return 0;
}
3、push()
push(x):将x入队,插入到队尾的位置
4、pop()
令队首元素出队
#include
#include
using namespace std;
int main(){
queue q;
for(int i = 1 ; i <= 5 ; i++)
q.push(i);
for(int i = 1 ; i <= 3 ; i++)
q.pop();
printf("%d",q.front());//4
return 0;
}
5、empty()
检测队列是否为空,如果是则返回true,不是则返回false
6、size()
返回队列中元素的个数
注意:在使用front()和pop()函数之前,必须使用empty()函数判断队列是否为空,否则可能出现错误
优先队列,底层是用堆来实现的,在优先队列中,队首元素一定是当前队列中优先级最高的那一个。使用的时候要添加#include< queue >以及using namespace std;
1、priority_queue的定义:
priority_queue name;
2、priority_queue容器内元素的访问:
与queue不同的是,优先队列没有front()和back()函数,只能够通过top()函数来访问队首元素
#include
#include
using namespace std;
int main(){
priority_queue q;
q.push(3);
q.push(4);
q.push(1);
printf("%d",q.top());//4
return 0;
}
3、push():
push(x):令x入队
4、top():
可以获得队首元素,实现容器内元素的访问
5、pop():
令队首元素出队
6、empty():
检测队列是否为空,若为空则返回true,否则返回false
7、size()
返回优先队列中元素的个数
8、元素优先级设置
(1)基本数据类型的优先级设置
以int类型为例,下面两种定义是等价的:
priority_queue q;
priority_queue,less > q;
第二种方式中vector< int >是来承载底层数据结构堆得容器,而less< int >则是对第一个参数的比较类,less< int >表示数字大的优先级越大,greater< int >表示数字小的优先级越大
#include
#include
using namespace std;
int main(){
priority_queue,greater > q;//数字小的优先级越大
q.push(3);
q.push(4);
q.push(1);
printf("%d",q.top());//1
return 0;
}
(2)结构体优先级设置
如用水果的名称和价格建立一个结构体,现在希望对水果价格高的优先极高,就需要重载“<”小于号,重载大于号会编译错误。
struct fruit{
string name;
int price;
friend bool operator < (fruit f1 , fruit f2){
return f1.price < f2.price;
}
};
注意:优先队列中的这个函数与sort中的cmp函数的效果是相反的,设置f1.price 示例:想以价格低的水果优先级高 **常见用途:**可以解决一些贪心问题,也可以对迪杰斯特拉算法进行优化 栈:后进先出的容器。想要使用栈,必须添加#include< stack >并加上using namespace std; 1、stack的定义: 2、元素的访问: 3、push(): 4、pop() 5、empty(): 6、size(): 当想要将两个元素绑在一起作为一个合成元素、又不想定义结构体时,可以使用pair。也就是说,pair实际上可以看做一个内部有两个元素的结构体。想使用pair,要添加#include< utility >以及using namespace std。 1、pair的定义: 2、元素的访问: 3、比较: 常见用途: max(x,y)、min(x,y)返回x、y中的最大值和最小值。且参数必须是两个,如果有三个参数,可以使用max(x,max(y,z))的写法。 swap(x,y)用来交换x、y的值 reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转 给出一个序列在全排列中的下一个序列。 可以将数组或容器中的某一段区间赋值为某个相同的值,与memset不同,这里的赋值可以是数组类型对应范围内的任何值 是用来排序的函数: 比较函数可以根据需要填写,如果不写,默认对前面给出的区间进行递增排序 例如:对于char型数组从大到小排序(按字典序) 如:对于结构体的排序定义,x不等时按照x从大到小排序,x相等时按照y从小到大排序 容器的排序:在STL中,只有vector、string、deque时可以使用sort的,因为set和map这种容器本身就有序 需要用在一个有序数组或者容器中。#include
三、stack
stack
stack中只能通过top()来访问栈顶元素
push(x):将x入栈
pop()用以弹出栈顶元素#include
检测stack是否为空,如果为空返回true,不为空则返回false
返回stack内的元素的个数四、pair
pair
pair中只有两个元素,分别为first和second#include
两个pair类型可以通过==、!=、<、<=、>、>=比较大小,规则是先以first的大小作为标准,只有当first相等时再去判别second的大小
#include
五、algorithm头文件下的常用函数
1、max()、min()、abs():
abs(x)返回x的绝对值。x必须是整数,如果是浮点数请用math头文件下的fabs2、swap():
3、reverse():
#include
4、next_permutation():
例如:当n=3时的全排列123
132
213
231
312
321
#include
在到达全排列的最后一个时next_permutation会返回false,这样会方便退出循环5、fill():
#include
6、sort():
sort(首元素地址,尾元素地址的下一个地址,比较函数)
#include
#include
#include
7、lower_bound()、upper_bound()
lower_bound(first,last,val)寻找在数组或容器的[first,last)范围内第一个值大于等于val的元素的位置,如果是数组则返回该位置的指针,如果是容器,则返回该位置的迭代器
upper_bound(first,last,val)寻找在数组或容器的[first,last)范围内第一个值大于val的元素的位置,如果是数组则返回该位置的指针,如果是容器,则返回该位置的迭代器
如果没有寻找到,则返回可以插入该元素的位置的指针或者迭代器#include