堆排(库函数)

来源:互联网 发布:windows qt4.8.0下载 编辑:程序博客网 时间:2024/05/21 08:38
#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () {  int a[] = {10,20,30,5,15};  vector<int> v(a,a+5);  vector<int>::iterator it;  make_heap (v.begin(),v.end());  cout << "initial max heap   : " << v.front() << endl;  pop_heap (v.begin(),v.end());  v.pop_back();  cout << "max heap after pop : " << v.front() << endl;  v.push_back(99);  v.push_back(222);  v.push_back(999);  make_heap (v.begin(),v.end());  cout << "max heap after push: " << v.front() << endl;  sort_heap (v.begin(),v.end());  cout << "final sorted range :";  for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];  cout << endl;  return 0;}

原创粉丝点击