一。sort的用法:
排序对象可以存在于普通数组中,也可以存在于vector中。前者用sort(a,a+n)来调用。后者用sort(v.begin(),v.end())的方式调用。
lower_bound的作用是查找“大于或者等于x的第一个位置”。
eg:lower_bound(a,a+n,x)
二。set
set是数学上的集合:每个元素最多只出现一次,和sort一样。set还会自动排序(按字典顺序)
如:set<string> dict;
三。映射:map
map就是从键到值的映射。例如可以用一个map<string,int> month_name来表示月份名字到月份编号的映射。然后用month_name["July"] = 7这样的方式来赋值。
1、头文件
#include<queue>
2、定义
- priority_queue<int> p;
3、优先输出大数据
priority_queue<Type, Container, Functional>
Type为数据类型, Container为保存数据的容器,Functional为元素比较方式。
如果不写后两个参数,那么容器默认用的是vector,比较方式默认用operator<,也就是优先队列是大顶堆,队头元素最大。
例如:
- #include<iostream>
- #include<queue>
- using namespace std;
- int main(){
- priority_queue<int> p;
- p.push(1);
- p.push(2);
- p.push(8);
- p.push(5);
- p.push(43);
- for(int i=0;i<5;i++){
- cout<<p.top()<<endl;
- p.pop();
- }
- return 0;
- }
输出:
4、优先输出小数据
方法一:
- priority_queue<int, vector<int>, greater<int> > p;
例如:
- #include<iostream>
- #include<queue>
- using namespace std;
- int main(){
- priority_queue<int, vector<int>, greater<int> >p;
- p.push(1);
- p.push(2);
- p.push(8);
- p.push(5);
- p.push(43);
- for(int i=0;i<5;i++){
- cout<<p.top()<<endl;
- p.pop();
- }
- return 0;
- }
输出:
方法二:自定义优先级,重载默认的 < 符号
例子:
- #include<iostream>
- #include<queue>
- #include<cstdlib>
- using namespace std;
- struct Node{
- int x,y;
- Node(int a=0, int b=0):
- x(a), y(b) {}
- };
- struct cmp{
- bool operator()(Node a, Node b){
- if(a.x == b.x) return a.y>b.y;
- return a.x>b.x;
- }
- };
- int main(){
- priority_queue<Node, vector<Node>, cmp>p;
- for(int i=0; i<10; ++i)
- p.push(Node(rand(), rand()));
- while(!p.empty()){
- cout<<p.top().x<<' '<<p.top().y<<endl;
- p.pop();
- }//while
- //getchar();
- return 0;
- }
输出: