我已经在一些奇怪的使用priority_queue的迷迷糊糊的,我很想获得为什么地球上它的plausibile /有效的priority_queue声明中使用这样的一些适当的解释:Priority_queue仿函数使用C++
typedef priority_queue<RandomContainer, **vector<RandomContainer>**, FunctorName> NewQueueName;
让我们说,我们已经得到了一些结构称为SPerson:
struct SPerson
{
int age;
string name;
string surname;
};
和一些仿函数,这将有助于队列中的所有元素进行排序因此我们likeing:
struct TheWayILike
{
bool operator()(const SPerson &name1, const SPerson &name2)
{
if(name1.name > name2.name) return true;
if(name1.name < name2.name) return false;
return false;
}
};
现在我们可以声明我们的priority_queue,它将基于结构中的元素,并将由函数称为TheWayILike的函数排序。
priority_queue<SPerson, TheWayILike>
,或者使用的typedef和单名像这样短的方式:
typedef priority_queue<SPerson, TheyWayILike> newNameForPQ;
但不知何故,这是错的,我必须添加以下行:矢量
问:
为什么我必须将我个人定制的数据类型变成矢量?
为什么它必须是一个矢量,为什么要用它呢?
为什么我需要将我的数据填充到矢量中?我没有在官方的priority_queue文档中阅读过它,所以我很乐意为菜鸟程序员获得一些易于理解的解释。
干杯!
'Container'只是一个模板参数。它告诉'priority_queue'在内部使用什么,你不必提供这个容器的一个实例!换句话说,像下面这样创建你的队列:'priority_queue,TheWayILike> myPQ'并将你的SPerson元素放入它中:'myPQ.push(a); myPQ.push(b)中; ...' –
2014-10-17 20:10:37