竞赛中 一点库的小知识

一、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>que;//采用默认优先级构造队列 默认为大根堆  
  
     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));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值