一、map
作用:我们可以用来当做hash,省去了很多代码量,不过毕竟库,有点慢
头文件 #Include<map>
声名 map<string,int>hash;
char s[100];
for(int i=1;i<=n;i++)
{
scanf("%s",s);
hash[s]=i;
} //将字符串s用map定义为整数;使用时 将hash直接看做数组 j=hash[s]
二、sort
详见 快排
三、priority_queue
作用:优先队列 实质是堆
头文件 #include<queue>
#include<vector>
struct cmp1{
bool operator ()(int &a,int &b){
return a>b;//最小值优先
}
};
struct cmp2{
bool operator ()(int &a,int &b){
return a<b;//最大值优先
}
};
//定义结构,使用运算符重载,自定义优先级2
struct number1
{
int x;
bool operator < (const number1 &a) const {
return x>a.x;//最小值优先
}
};
struct number2
{
int x;
bool operator < (const number2 &a) const {
return x<a.x;//最大值优先
}
};
priority_queue<int,vector<int>,cmp1>que1;//最小值优先
priority_queue<int,vector<int>,cmp2>que2;//最大值优先
priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
//这是右移运算符,所以这里用空格号隔开
priority_queue<int,vector<int>,less<int> >que4;
//最大值优先
priority_queue<number1>que5;
priority_queue<number2>que6;
操作:
pop();
入队
top();
访问队顶值
push();
弹出队顶值
对于双元素队 还有一种写法
priority_queue<pair<int, int> > thePair;
push(make_pair(x,y));