queue/priority_queue笔记
需要头文件#include <queue>
queue<int> q;
q.push(12);
while(!q.empty() )
{
cout << q.front() <<endl;
q.pop();
}
priority_queue <int> q; //默认按照从大到小排序
q.push(1);
q.push(2);
while(!q.empty() )
{
cout <<q.top() <<endl;
q.pop();
}
priority_queue<int, vector<int> , greater<int> > q; //按照从小到大的顺序
q.push (23);
q.push(22);
q.push (2);
while(!q.empty() )
{
cout << q.top() << endl;
q.pop();
}
对于自定义的对象,需要自己实现比较操作
struct Score
{
int score_;
string name_;
Score(int score, const string name)
:score_(score), name_(name)
{ }
};
class Cmp
{
public:
bool operator() (const Score &s1, const Score &s2)
{
return s1.score_ < s2.score_;
}
};
// Cmp p;
// p(s1, s2)
int main(int argc, const char *argv[])
{
priority_queue<Score, vector<Score>, Cmp> q;
q.push(Score(67, "zhangsan"));
q.push(Score(88, "lisi"));
q.push(Score(34, "wangwu"));
}

浙公网安备 33010602011771号