std::priority_queue使用示例 STL

简单的使用示例。

#include "stdafx.h"//非vc编译器可去掉 #include <iostream> #include <vector> #include <queue> using namespace std; //看看priority_queue的声明,模板的第三个参数就是比较规则,规则可以是个函数,也可是个仿函数 //template < class T, class Container = vector<T>, // class Compare = less<typename Container::value_type> > class priority_queue; struct Node { int frequery; int tr; friend bool operator> (const Node& lhs, const Node& rhs) { return (lhs.frequery > rhs.frequery); } }; struct node_greater_comp { bool operator()(const Node& lhs, const Node& rhs) const { return lhs.frequery > rhs.frequery; } }; int main() { //示例一 { priority_queue < Node, vector<Node>, greater<Node> > q; //仿函数greater中用到operator> Node w[10]; w[0].frequery=12; w[1].frequery=32; w[2].frequery=34; q.push(w[0]); q.push(w[1]); q.push(w[2]); while(!q.empty()) { cout <<q.top().frequery <<" "; q.pop(); } cout <<endl; } //示例二 { Node w[3]; w[0].frequery=12; w[1].frequery=32; w[2].frequery=34; priority_queue < Node, vector<Node>, node_greater_comp > q(w,w+3); //仿函数node_greater_comp是比较规则 while(!q.empty()) { cout <<q.top().frequery <<" "; q.pop(); } cout <<endl; } return 0; }

你可能感兴趣的:(struct,Class,编译器)