cdcq

梦幻小鱼干

导航

STL集合

善用轮子,拒绝做工具人

我再写手写堆我就是狗

 

O 万能头

#include<bits/stdc++.h>

O 优先队列

库:queue vector(记得using namespace std;)

声明:

priority_queue<int> h;

不加参数时堆顶是最大的元素

但是重载运算符只能重载小于号,很奇怪

重载运算符可以这么写

friend bool operator<(nds x,nds y){

  return x.z>y.z;

}

这样写出来堆顶的元素是z最小的

priority_queue<int,vector<int>,greater<int> >h;

这个跟默认没啥区别

priority_queue<int,vector<int>,less<int> > h;

这个和默认反过来

priority_queue<int,vector<int>,cmp> h;

cmp是比较器,可以这么写

struct cmp{

  bool operator()(int x,int y){

    return x>y;

  }

};

注意operator后边有一对括号(),据说默认比较器就是这玩意

它相当于小于号,需要注意比较器是小于号,而堆顶是最大的元素

操作:

empty() 如果队列为空返回true

push() pop() size() 不说了

top() 返回堆顶元素

 

O map

库:map

声明:

map<int,int> f;

第一个是索引(即下标),第二个是里边装的东西的类型

map<int,map<int,int> > f;

这个可以当二维数组

操作:

直接当数组用就vans了

map.insert(1, 2) 插入key为1value位2的元素,如果存在key为1的元素则插入失败

map.count(1) 检查是否存在key为1的元素

map.erase(1) 删除key为1的元素

map.begin() map.end() 返回迭代器

从小到大遍历map:

for(map<int,int>::iterator i=a.begin();i!=a.end();i++)

反过来:

for(map<int,int>::reverse_iterator i=a.end();i!=a.begin();i++)

取一个迭代器i的key和value:

i->first i->second

 

O next_permutation

库:algorithm

操作:next_permutation(a+1,a+n+1);

posted on 2020-03-03 16:19  cdcq  阅读(174)  评论(0编辑  收藏  举报