一:用法示例
一共两个重载:
default (1)
template <class RandomAccessIterator>
void push_heap ( RandomAccessIterator first , RandomAccessIterator last ) ;
custom (2)
template <class RandomAccessIterator, class Compare>
void push_heap ( RandomAccessIterator first , RandomAccessIterator last , Compare comp ) ;
在说明这个函数的功能之前,我们先来看看对它的英文解释:
Push element into heap range
Given a heap in the range
[first,last-1)
, this function extends the range considered a heap to
[first,last)
by placing the value in
(last-1)
into its corresponding location within it.
大概意思是,push_heap的作用范围是[ first , last),在使用push_heap必须保证[ first , last-1)满足堆,而*(last-1)是新增在末尾的一个值,那么此时对于整个[ first , last ) , 因为*(last-1)的新增可能已经不满足堆。push_back的作用就是重新调整*(last-1)的位置,使其整个[ first , last )满足堆。
例子:
二:源码及剖析
源码方面的话,其实一点都不难,一共4个模板函数,重点是第一个,其余三个都是基于第一个模板函数的。
源码摘抄自Visual Studio 2015安装目录algorithm文件中。
点击进入目录----> C++源码剖析目录