您的位置:首页 > 编程语言 > C语言/C++

算法设计、分析与实现 从入门到精通 C、C++和Java 这本书的堆实现85页C++语言实现有问题

2012-05-29 22:16 821 查看
在第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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: