优先级队列和结构

时间:2011-05-17 13:06:31

标签: c++ struct priority-queue

#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。但我想单独使用它们。

我是说如何打印到屏幕只调用callTime?例如:q.top(callTime);或其他什么?谁能帮我?

1 个答案:

答案 0 :(得分:4)

您只是在寻找:

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

等等?