C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)

有的时候为了维护数据,我们使用stl的堆去维护一序列。

首先您要弄清楚堆和栈的区别,即heap和stack

stl中的堆默认是最大堆。

先介绍 push_heap,pop_heap,make_heap,sort_heap这四个算法,这四个不是C++11新增加的内容。

首先是如何产生一个最大推:
make_heap
原型:

template <class RandomAccessIterator>
  void make_heap (RandomAccessIterator first, RandomAccessIterator last);   
template <class RandomAccessIterator, class Compare>
  void make_heap (RandomAccessIterator first, RandomAccessIterator last,
                  Compare comp );

第一个就是默认的,最大堆。
作用:
Rearranges the elements in the range [first,last) in such a way that they form a heap.

push_heap,pop_heap
这两个就不再赘述,在代码里有显现。

sort_heap
Sort elements of heap

看一段上面四个方法的应用:

#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector

int main() {
    int myints[] = { 10,20,30,5,15 };
    std::vector<int> v(myints, myints + 5);

    std::make_heap(v.begin(), v.end());
    std::cout << "initial max heap : " << v.front() << '\n';

    std::pop_heap(v.begin(), v.end()); 
    v.pop_back();
    std::cout << "max heap after pop : " << v.front() << '\n';

    v.push_back(99); 
    std::push_heap(v.begin(), v.end());
    std::cout << "max heap after push: " << v.front() << '\n';

    std::sort_heap(v.begin(), v.end());

    std::cout << "final sorted range :";
    for (unsigned i = 0; i<v.size(); i++)
        std::cout << ' ' << v[i];

    std::cout << '\n';

    return 0;
}
//输出:
//initial max heap : 30
//max heap after pop : 20
//max heap after push : 99
//final sorted range : 5 10 15 20 99

接下来看看在上面的基础上,C++11又新增加了哪些内容:
is_heap
作用:
Returns true if the range [first,last) forms a heap, as if constructed with make_heap.
应用:

#include <iostream> // std::cout
#include <algorithm> // std::is_heap, std::make_heap, std::pop_heap
#include <vector> // std::vector

int main () {
  std::vector<int> foo {9,5,2,6,4,1,3,8,7};

  if (!std::is_heap(foo.begin(),foo.end()))
    std::make_heap(foo.begin(),foo.end());

  std::cout << "Popping out elements:";
  while (!foo.empty()) {
    std::pop_heap(foo.begin(),foo.end());   // moves largest element to back
    std::cout << ' ' << foo.back();         // prints back
    foo.pop_back();                         // pops element out of container
  }
  std::cout << '\n';

  return 0;
}

is_heap_until
作用:
Find first element not in heap order
Returns an iterator to the first element in the range [first,last) which is not in a valid position if the range is considered a heap (as if constructed with make_heap).

应用:

#include <iostream> // std::cout
#include <algorithm> // std::is_heap_until, std::sort, std::reverse
#include <vector> // std::vector

int main () {
  std::vector<int> foo {2,6,9,3,8,4,5,1,7};

  std::sort(foo.begin(),foo.end());
  std::reverse(foo.begin(),foo.end());

  auto last = std::is_heap_until (foo.begin(),foo.end());

  std::cout << "The " << (last-foo.begin()) << " first elements are a valid heap:";
  for (auto it=foo.begin(); it!=last; ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

注:例子来源于www.cplusplus.com网站

你可能感兴趣的:(C++11)