std :: priority_queue与std :: pair <int,int =“”>

时间:2017-02-07 21:10:29

标签: c++ c++11 priority-queue std-pair

您好我有以下代码:

#include <algorithm>
#include <queue>
#include <functional>
std::priority_queue < std::pair<int, int>, std::greater<std::pair<int, int> > > q;

我正在尝试使用最小堆功能。 我现在搜索了几个小时仍然无法得到明确的答案。 我见过很多人编写自定义比较函数,但我认为这是非常标准的操作。如果我取出std :: greater func,一切都按预期工作,但它会创建一个最大堆。

编译时我得到14个错误。

Error   C2039   'value_type': is not a member of 'std::greater<std::pair<int,int>>' 
Error   C2146   syntax error: missing '>' before identifier 'value_type'    
Error   C2039   'value_type': is not a member of 'std::greater<std::pair<int,int>>' 
Error   C3646   'value_type': unknown override specifier    
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'size_type': is not a member of 'std::greater<std::pair<int,int>>'  
Error   C3646   'size_type': unknown override specifier     
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'reference': is not a member of 'std::greater<std::pair<int,int>>'  
Error   C3646   'reference': unknown override specifier
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
Error   C2039   'const_reference': is not a member of 'std::greater<std::pair<int,int>>'    
Error   C3646   'const_reference': unknown override specifier   
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int

1 个答案:

答案 0 :(得分:3)

hey' for main:Object (NameError)的第二个模板参数是要使用的基础容器类型,而不是要使用的比较运算符。默认为priority_queue,您可以明确指定:

std::vector<T>