简体   繁体   中英

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'. What may be the reason? I use RedHat Linux with gcc. Thanks a lot.

You are missing parentheses near begin() and end() and are defining comparator the wrong way. 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; 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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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