#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
  vector<int> vec1;
  vector<int>::iterator vec_iter1;

  for (int k=0;k<10;k++)
  {
    vec1.push_back(rand());
  }

  for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  make_heap(vec1.begin(), vec1.end());

  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  //取出堆中最大的数放到最后面,剩下的重新排
  pop_heap(vec1.begin(), vec1.end());

  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  //删除最后一个
  vec1.pop_back();

  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  vec1.push_back(100);
  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  push_heap(vec1.begin(),vec1.end());
  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  sort_heap(vec1.begin(), vec1.end());
  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "-------------------------------------------------------" << endl;

  system("pause");
  return 0;
}

=============================================================

41 18467 6334 26500 19169 15724 11478 29358 26962 24464
-------------------------------------------------------
29358 26962 15724 26500 24464 6334 11478 41 18467 19169
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 29358
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 100
-------------------------------------------------------
26962 26500 15724 19169 24464 6334 11478 41 18467 100
-------------------------------------------------------
41 100 6334 11478 15724 18467 19169 24464 26500 26962
-------------------------------------------------------
请按任意键继续. . .

05-11 22:52