priority_queue和sort应用
#include"iostream" #include"String" #include"stdio.h" #include "string.h" #include <queue> #include <algorithm> using namespace std; class Number{ public: int a; bool operator <(const Number &m) const{ return a > m.a; } }; priority_queue<Number> q; bool compare(int a,int b) { return a > b;//如果是>就是从大到小,是<就是从小到大 } int main() { Number num[5]; num[0].a = 3; num[1].a = 5; num[2].a = 2; num[3].a = 9; num[4].a = 1; int i; for(i = 0;i < 5;i++) q.push(num[i]); while(!q.empty()) { cout<<q.top().a<<"\t"; q.pop(); } cout<<endl; int a[5] = {3,5,2,9,1}; sort(a,a+5,compare); for(i = 0 ;i < 5; i++) cout<<a[i]<<"\t"; }
结果如下