duifa3.cpp

来自「数据结构算法 vc++6.0 的程序集包含所有章节,适合学习数据结构,把数据结构」· C++ 代码 · 共 38 行

CPP
38
字号
// 堆排序法(迭代器)duifa3.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include<stdlib.h>
#include<time.h>
using namespace std;
void main()
{cout<<"duifa3.cpp运行结果:\n";
 const int SIZE=10;int i,a[SIZE];
 srand(time(0));
 for(i=0;i<SIZE;i++)
  a[i]=rand()%80;
 vector<signed int>v(a,a+SIZE),v2;
 ostream_iterator<int> output(cout," ");
 cout<<"Vector v before make_heap:\n";
 copy(v.begin(),v.end(),output);
 make_heap(v.begin(),v.end());
 cout<<"\nVector v after make_heap:\n";
 copy(v.begin(),v.end(),output);
 sort_heap(v.begin(),v.end());
 cout<<"\nVector v after sort_heap:\n";
 copy(v.begin(),v.end(),output);
 // perform the heapsort with push_heap and pop_heap
 cout<<"\nArray a contains: ";
 copy(a,a+SIZE,output);
 for(i=0;i<SIZE;++i) {
   v2.push_back(a[i]);
   push_heap(v2.begin(),v2.end());
   cout<<"\nv2 after push_heap(a["<<i<<"]): ";
   copy(v2.begin(),v2.end(),output);}
 for(i=0;i<(int)v2.size();++i) {
   cout<<"\nv2 after "<<v2[0]<<" popped from heap\n";
   pop_heap(v2.begin(),v2.end()-i);
   copy(v2.begin(),v2.end(),output);}
 cout<<endl;cin.get();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?