1.queue队列
头文件#include <queue>
queue<int>q;
queue<double>q;
queue<node>q;
入队列:q.push(x)将x元素放入队列的末端
出队列:q.pop()将第一个元素删除
访问队首元素:q.front
访问队中的元素的个数: q.size();
2.priority_queue优先队列priority模板类有三个参数,第一个是元素类型,第二个是容器类型,第三个是比较算子
priority_queue<int>q;
priority_queue<int,vector<int>,greater<int>>q;从小到大排列
priority_queue<int,vector<int>,less<int>>q;从大到小排列
访问优先队列的队首元素 q.top() ;
出队列: q.pop();
入队列: q.push(x);
判断优先队列是否为空: q.empty();
3.stack栈
stack的头文件 <stack>.
stack<int> s1;
stack<string> s2;
Stack 操作:
入栈 :q.push(x);
出栈 :q.pop(); 注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶 :q.top();
判断栈空 :q.empty(); 当栈空的时, 返回true。
访问栈中的元素个数 :q.size();
Stack 操作:
入栈 :q.push(x);
出栈 :q.pop(); 注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶 :q.top();
判断栈空 :q.empty(); 当栈空的时, 返回true。
访问栈中的元素个数 :q.size();
7.map
map是键-值对的集合。map类型通常可理解为关联数组。
map的头文件 :#include<map>;
map对象的定义:
map<string,int>q; map<int,int>q; map<string,node>q; map<int,node>; map<int,string>q;
map添加元素:
如:map<int,string>q; q[100]=”adnsnd”;
还可以:q.insert(pair<int,string>(100,”adnsnd”));
q.insert(map<int,string>::value_type(100,”adnsnd”)) ;
map查找并读取元素:
如map<int,string>q;
最简单的方法:int n=q[“dadad”];
7.set
头文件:#include<set>
set对象的定义:set<int>ivec;
set中添加元素:
ivec.insert(10);
set中获取元素
ivec.find(x);
判断x是否在ivec中出现过可以用:
ivec.find(x); 也可以用 ivec.count(x);这里count的返回值只能是1或0。
set的遍历;
set<int>::iterator it=ivec.begin();
While(it!=q.end())
{
printf(“%d ”,*it);
it++;}
set的删除元素:
it=ivec.find(x);
ivec.erase(it);
set 的lower_bound/upper_bound的用法:
使用迭代器 set<int>::iterator itlow,itup;
itlow=ivec.lower_bound(x);
itup=ivec.upper_bound(x);
lower_bound返回的是在ivec中大于或等于x的第一个数的位置,upper_bound返回的是在ivec中大于x的第一个数的位置;