在第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);
}