C++ STL(22):heap堆操作

#include 
#include 
#include 
#include 
#include 
#include 
 
//堆操作
/*
     原始堆中的每个元素都以任意顺序安插,但移除的顺序是由最大到最小,
     堆内部以树状方式来表现,该树的结构以“每个节点小于或者等于其父节点”的方式构造
     属性:
          1、在未排序的堆中,第一个元素总是最大的,即*first便是堆中最大的元素
          2、在对数时间内以push_heap为堆增加新元素,以pop_heap为堆移除元素
          3、可以通过sort_heap方法将堆转成sorted range
*/
int main()
{
     /************************************************************************/
     //make_heap
     /************************************************************************/
     /*
          make_heap:将range[first,last)转换为一个堆
          版本1:采用operator<进行比较,必导致is_heap(first,last)为true
          版本2:采用function object comp进行比较,必导致is_heap(first,last,comp)为true
     */
     /*
     template
     void make_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last
      );
     template
     void make_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last,
          BinaryPredicate _Comp
      );
      */
     int a[] = { 1, 3, 5, 2, 9, 6, 8 };
     std::make_heap(std::begin(a), std::end(a));
     assert(std::is_heap(std::begin(a), std::end(a)));    //true
     //9 3 8 2 1 6 5
     std::copy(std::begin(a), std::end(a), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     //使第一个元素为最小
     std::make_heap(std::begin(a), std::end(a), std::greater());
     assert(std::is_heap(std::begin(a), std::end(a), std::greater()));    //true
     //1 2 5 9 3 6 8
     std::copy(std::begin(a), std::end(a), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     //将堆进行排序,由于上述make_heap操作使用了greater(),所以在sort_heap函数的第三个参数设置为greater()
     std::sort_heap(std::begin(a), std::end(a), std::greater());
     assert(std::is_sorted(std::begin(a), std::end(a), std::greater()));    //true
     //9 8 6 5 3 2 1
     std::copy(std::begin(a), std::end(a), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     /************************************************************************/
     //push_heap
     /************************************************************************/
     //push_heap:为堆增加新元素以重新生成堆
     /*
     template
     void push_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last
      );
     template
     void push_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last,
          BinaryPredicate _Comp
      );
      */
     std::vector iv;
     for (auto i : { 1, 2, 3, 4, 5, 6, 7, 8, 9})
          iv.push_back(i);
     //全排列中的一组
     //9 2 7 3 1 6 8 4 5
     std::random_shuffle(iv.begin(), iv.end());
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     std::make_heap(iv.begin(), iv.end());
     //9 5 8 4 1 6 7 2 3
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     iv.push_back(10);
     std::push_heap(iv.begin(), iv.end());
     //10 9 8 4 5 6 7 2 3 1
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     /************************************************************************/
     //pop_heap
     /************************************************************************/
     //pop_heap:为堆移除最大(greater()为最小)元素,把移除的元素移到最后
     /*
     template
     void pop_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last
      );
     template
     void pop_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last,
          BinaryPredicate _Comp
      );
      */
     iv.clear();
     for (auto i : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })
          iv.push_back(i);
     std::random_shuffle(iv.begin(), iv.end());
     std::make_heap(iv.begin(), iv.end());
     //9 8 7 6 2 3 5 1 4
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     //把*first移动最后
     std::pop_heap(iv.begin(), iv.end());
     //8 6 7 4 2 3 5 1 9
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     std::make_heap(iv.begin(), iv.end(), std::greater());
     //1 2 3 4 8 7 5 6 9
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     //把*first移动最后
     std::pop_heap(iv.begin(), iv.end(), std::greater());
     //2 4 3 6 8 7 5 9 1
     std::copy(iv.begin(), iv.end(), std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;
 
     //sort_heap的实现方式
     int c[] = { 1, 2, 3, 4, 5, 6 };
     const int nc = sizeof c / sizeof c[0];
     int n = 0;
     /*
          6 5 3 4 2 1
          5 4 3 1 2
          4 2 3 1
          3 2 1
          2 1
          1
     */
     std::make_heap(std::begin(c), std::end(c));
     while (n < nc)
     {
          std::copy(std::begin(c), std::end(c) - n, std::ostream_iterator(std::cout, " "));
          std::cout << std::endl;
          std::pop_heap(std::begin(c), std::end(c) - n);
          n++;
     }
     std::copy(std::begin(c), std::end(c) - n, std::ostream_iterator(std::cout, " "));
     std::cout << std::endl;

     /************************************************************************/
     //sort_heap
     /************************************************************************/
     //sort_heap:把堆[first,last)转成一个sorted range,不是一个stable稳定的排序法
     /*
     template
     void sort_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last
      );
     template
     void sort_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last,
          Predicate _Comp
      );
      */
     //略
 
     /************************************************************************/
     //is_heap
     /************************************************************************/
     //is_heap:判断是否为堆上的range
     /*
     template
     bool is_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last
      );
     template
     bool is_heap(
          RandomAccessIterator _First,
          RandomAccessIterator _Last,
          BinaryPredicate _Comp
      );
      */
     //略
     return 0;
}

====================打个广告,欢迎关注====================

QQ: 412425870
csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)



你可能感兴趣的:(C++,STL,C++,STL函数)