优先级队列和结构

priority queue and structs

本文关键字:结构 队列 优先级      更新时间:2023-10-16
#include <iostream>
#include <queue>
using namespace std;
struct Call
{
    Call( int callNum, long callTime, int callLength ) :
    CallNum( callNum ), CallTime( callTime ), CallLength( callLength ) { }
    int CallNum;
    long CallTime;
    int CallLength;
};
bool operator>( const Call& lhs, const Call& rhs ) {
    return lhs.CallLength > rhs.CallLength;
}
ostream& operator<<( ostream& os, const Call& c ) {
    os << c.CallNum << " " << c.CallTime << " " << c.CallLength;
    return os;
}
int main()
{
    priority_queue< Call, vector<Call>, greater<Call> > q; 
    q.push( Call( 1, 200, 150 ));
    q.push( Call( 2, 300, 950 ));
    q.push( Call( 3, 100, 450 ));
    q.push( Call( 4, 150, 320 ));
    unsigned i=0, n=q.size();
    for ( i=0; i<n; ++i ) {
        cout << q.top() << endl;
        q.pop();
    }
}

这是我的代码。我的问题是,当我使用q.top();时,它会打印到屏幕callNum, callTime, callLength。但我想单独使用它们。

我的意思是我怎样才能打印到屏幕只是呼叫时间? 例如:q.top(callTime);还是别的什么? 谁能帮助我?

你只是在寻找:

cout << q.top().CallNum << endl;

等等?