在第86页的代码中少了#include <functional>这个头文件,编译时报如下错误:#include <iostream>
1>f:\算法导论\二叉堆\二叉堆\main.cpp(12) : error C2065: 'less' : undeclared identifier
1>f:\算法导论\二叉堆\二叉堆\main.cpp(12) : error C2062: type 'int' unexpected
1>f:\算法导论\二叉堆\二叉堆\main.cpp(17) : error C2065: 'greater' : undeclared identifier
1>f:\算法导论\二叉堆\二叉堆\main.cpp(17) : error C2062: type 'int' unexpected
加上该头文件后编译通过。
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
int h[]={4,1,3,2,16,9,10,14,8,7,10,23,44,1,5,6,9,7,2,5,6,3,15,200,1,2,0,33,999};
int Num=sizeof(h)/sizeof(h[0]);
make_heap(h,h+Num,less<int>());
cout<<"max heap:"<<endl;
copy(h,h+Num,ostream_iterator<int>(cout," "));
cout<<endl;
make_heap(h,h+Num,greater<int>());
cout<<"min heap:"<<endl;
copy(h,h+Num,ostream_iterator<int>(cout," "));
cout<<endl;
return (EXIT_SUCCESS);
}
本文解决了一个在实现二叉堆时因缺少头文件导致的编译错误问题,通过添加`<functional>`头文件,使得程序能够正确编译并运行。演示了如何使用C++中的`make_heap`, `less<int>()`, `greater<int>()`等函数进行堆操作。

被折叠的 条评论
为什么被折叠?



