简体   繁体   English

make_heap()编译问题

[英]make_heap() compilation issue

compiling following: 编译如下:

class Compare
{
   bool cmp(const int& a, const int& b){return a>b;}
};

int main()
{
   vector<int, Compare> v;
   make_heap(v.begin(), v.end(), Compare());
}

result in compilation error - no class template named 'rebind' in 'class Compare'. 导致编译错误-在“类比较”中没有名为“ rebind”的类模板。 What may be the reason? 可能是什么原因? I use RedHat Linux with gcc. 我将RedHat Linux与gcc一起使用。 Thanks a lot. 非常感谢。

You are missing parentheses near begin() and end() and are defining comparator the wrong way. 您在begin()end()附近缺少括号,并且以错误的方式定义比较器。 This is what it should probably look like: 它可能看起来像这样:

#include <vector>
#include <algorithm>
#include <functional>

struct Compare: std::binary_function<int const&, int const&, bool>
{
   public:
   bool operator()(const int& a, const int& b){return a>b;}
};

int main()
{
   std::vector<int> v;
   std::make_heap(v.begin(), v.end(), Compare());
   return 0;
}

std::vector<> doesn't have a comparator template argument; std :: vector <>没有比较器模板参数; it has an allocator for its second argument. 它的第二个参数有一个分配器

You're using your comparator as an allocator in the vector template argument list. 您正在将比较器用作矢量模板参数列表中的分配器。

class Compare
{
   public:
   bool operator()(const int& a, const int& b){return a>b;}
};

int main()
{
   vector<int> v;
   make_heap(v.begin(), v.end(), Compare());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM