由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教C++ STL中priority_queue模板参数中的Compare函数
相关主题
基本功不扎实,问个问题*(&b1)=b编译不过,b1=b可以,区别是?
C++糟粕和需要避免的。IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (
问个函数指针指向操作符的问题override operator[] inline
请问释放容器内存的方法c++的问题就是用*标识指针,和乘法混了,应该用@替代
C++编程问题:union inside struct有没有会自动聚合的操作符重载或宏?
一个C的void指针的问题强迫症爱好者进来做题了
a[i]=i++【讨论】为什么要用友员来实现算符重载?
C++的"初始化"小结简单c++问题,大家练练手
相关话题的讨论汇总
话题: event话题: queue话题: priority话题: pevent1
进入Programming版参与讨论
1 (共1页)
R*******y
发帖数: 19
1
我有一个Event类型的结构体,已经重载了比较操作符并且运行正确。为了设计上的需
要,我要写一个priority_queue,保存Event类的指针(注意不是Event类本身)。声明
如下:
priority_queue, CompEventByPtr> m_EventQue;
CompEventByPtr实现如下:
bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (*pEvent1 < *pEvent2);
}
系统编译报错:
error C2923: 'std::priority_queue' : 'CompEventByPtr' is not a valid
template type argument for parameter '_Pr'
我不明白为什么这不是合法的比较函数。我将实现改为:
bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (pEvent1->time >
t****t
发帖数: 6806
2
you need to provide a functor (a CLASS) for this template argument, not a
function.

【在 R*******y 的大作中提到】
: 我有一个Event类型的结构体,已经重载了比较操作符并且运行正确。为了设计上的需
: 要,我要写一个priority_queue,保存Event类的指针(注意不是Event类本身)。声明
: 如下:
: priority_queue, CompEventByPtr> m_EventQue;
: CompEventByPtr实现如下:
: bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
: {
: return (*pEvent1 < *pEvent2);
: }
: 系统编译报错:

p***o
发帖数: 1252
3
Function is OK. He need to specify the type of the function in the template.

【在 t****t 的大作中提到】
: you need to provide a functor (a CLASS) for this template argument, not a
: function.

X****r
发帖数: 3557
4
That could work, but --
You have to make sure you pass the actual function pointer
when creating the priority queue. Since a function pointer type
is considered primitive type and the default 'constructor' does
nothing, it is possible to create the priority queue with the
'default' compare function which would crash the application.

template.

【在 p***o 的大作中提到】
: Function is OK. He need to specify the type of the function in the template.
o**o
发帖数: 3964
5
?

【在 X****r 的大作中提到】
: That could work, but --
: You have to make sure you pass the actual function pointer
: when creating the priority queue. Since a function pointer type
: is considered primitive type and the default 'constructor' does
: nothing, it is possible to create the priority queue with the
: 'default' compare function which would crash the application.
:
: template.

X****r
发帖数: 3557
6
Compiler probably won't warn you for the following definition:
priority_queue, bool (*)(Event*,Event*)>
m_EventQue;
However if you try to use m_EventQue.pop(), your code will crash.
The right definition is:
priority_queue, bool (*)(Event*,Event*)>
m_EventQue(CompEventByPtr);

【在 o**o 的大作中提到】
: ?
1 (共1页)
进入Programming版参与讨论
相关主题
简单c++问题,大家练练手C++编程问题:union inside struct
这段code有啥问题?一个C的void指针的问题
重载 ^ 操作符编译出错a[i]=i++
关于 expression template 这种叫法C++的"初始化"小结
基本功不扎实,问个问题*(&b1)=b编译不过,b1=b可以,区别是?
C++糟粕和需要避免的。IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (
问个函数指针指向操作符的问题override operator[] inline
请问释放容器内存的方法c++的问题就是用*标识指针,和乘法混了,应该用@替代
相关话题的讨论汇总
话题: event话题: queue话题: priority话题: pevent1