sort
头文件 <algorithm>
sort函数可以不使用参数进行排序,即
sort(a,a+n);
这种排序是一种升序排序,想要自定义排序方式,则需要改写sort函数的参数
例:
int a[N];
bool cmp(int a,int b)
{
return a>b;
}
sort(a,a+n,cmp);
这种排序的结果是一种降序排序,在该例中自定义cmp参数设定为a>b时返回真,最终得到到的结果也返回从大到小的数组。
当排序对象为结构体时,可通过自定义cmp参数来对结构体进行特定对象的排序
例
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int a,b;
}a[100];
bool cmp (node a,node b)
{
return a.a>b.a;
}
int main()
{
for(int i=0,l=10;i<=10;i++,l--)
{
a[i].a=i;
a[i].b=l;
}
for(int i=0;i<=10;i++)
{
printf("%d %d\n",a[i].a,a[i].b);
}
sort(a,a+11,cmp);
for(int i=0;i<=10;i++)
{
printf("%d %d\n",a[i].a,a[i].b);
}
return 0;
}
结果:
0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
10 0
10 0
9 1
8 2
7 3
6 4
5 5
4 6
3 7
2 8
1 9
0 10
priority_queue
引用自
ACM向:关于优先队列priority_queue自定义比较函数用法整理
模板声明带有三个参数,
priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大。
priority_queue默认为大顶堆,即堆顶元素为堆中最大元素。如果我们想要用小顶堆的话需要增加使用两个参数:
priority_queue<int, vector<int>, greater<int> > q; // 小顶堆
priority_queue<int, vector<int>, less<int> > q; // 大顶堆
1.重载bool operator<,写在结构体外面
#include<queue>
#include<iostream>
using namespacestd;
struct node
{
int x, y;
node(int x=0, int y=0):x(x),y(y){}
};
booloperator<(node a, node b)
{
if(a.x > b.x) return 1;
else if(a.x == b.x)
if(a.y >= b.y) return 1;
return 0;
}
int main()
{
priority_queue<node> pq;
pq.push(node(1,2)); pq.push(node(2,2));
pq.push(node(2,3)); pq.push(node(3,3));
pq.push(node(3,4)); pq.push(node(4,4));
pq.push(node(4,5)); pq.push(node(5,5));
while(!pq.empty())
{
cout<<pq.top().x<<pq.top().y<<endl;
pq.pop();
}
return 0;
}
或者写成 const node &a
bool operator < (const node &a,const node &b)
{
if(a.x > b.x) return 1;
else if(a.x == b.x)
if(a.y >= b.y) return 1;
return 0;
}
或者写成 const node a
bool operator < (const node a,const node b)
{
if(a.x > b.x) return 1;
else if(a.x == b.x)
if(a.y >= b.y) return 1;
return 0;
}
但不能写成 node &a 的形式
2、重载bool operator<,写在结构体里面
要是直接写在里面就只需要一个参数,但是后面必须加const修饰,否则报错,如:
bool operator < (const node &b) const
同样const node &a和node a都可以,但node &a不可以
#include<queue>
#include<iostream>
using namespacestd;
struct node
{
int x, y;
node(int x=0, int y=0):x(x),y(y){}
bool operator<(const node &b) const
{
if(x > b.x) return 1;
else if(x == b.x)
if(y >= b.y) return 1;
return 0;
}
};
int main()
{
priority_queue<node> pq;
pq.push(node(1,2)); pq.push(node(2,2));
pq.push(node(2,3)); pq.push(node(3,3));
pq.push(node(3,4)); pq.push(node(4,4));
pq.push(node(4,5)); pq.push(node(5,5));
while(!pq.empty())
{
cout<<pq.top().x<<pq.top().y<<endl;
pq.pop();
}
return 0;
}
友元必须要写在里面,且是两个参数,同样node &a不可以,const node &a和node a都可以,但是无需const在函数最后修饰,否则报错
struct node
{
int x, y;
node(int x=0, int y=0):x(x),y(y){}
friend bool operator<(const node&a, const node &b)
{
if(a.x > b.x) return 1;
else if(a.x == b.x)
if(a.y >= b.y) return 1;
return 0;
}
};
3、可以自定义一个比较类,Compare
priority_queue中的三个参数,后两个可以省去,因为有默认参数,不过如果有第三个参数的话,必定要写第二个参数。
而且这个是一个类,这个类里重载operator(),和自定义sort排序不同,sort只需bool cmp(……)(当然sort也可以弄一个比较类,再重载operator()),若是priority_queue中写为sort的cmp形式则报错,如:bool cmp1(const node &a, const node&b)//报错!
#include <queue>
#include <iostream>
using namespace std;
struct node
{
int x, y;
node(intx=0, int y=0):x(x),y(y){}
};
struct cmp
{
booloperator()(const node &a, const node &b)
{
if(a.x> b.x) return 1;
elseif(a.x == b.x)
if(a.y>= b.y) return 1;
return0;
}
};
int main()
{
priority_queue<node,vector<node>, cmp> pq; //注意这里的写法
pq.push(node(1,2)); pq.push(node(2,2));
pq.push(node(2,3)); pq.push(node(3,3));
pq.push(node(3,4)); pq.push(node(4,4));
pq.push(node(4,5)); pq.push(node(5,5));
while(!pq.empty())
{
cout<<pq.top().x<<pq.top().y<<endl;
pq.pop();
}
return0;
}
值得注意的是,这个比较类里node&a,const node &a和node a都可以了,重载bool operator() 最后加const修饰也可以。