优先级队列和结构体
#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);
或者其他什么?谁能帮助我?
#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();
}
}
This is my code. My problem is, When I use q.top();
it prints to screen callNum, callTime, callLength
. But I want to use them seperately.
I mean How can I print to screen just callTime? for ex: q.top(callTime);
or something else? who can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只是在寻找:
等等吗?
Are you just looking for:
and so on?