Heap堆的数据结构是经常使用,Heap它还能够存储元件的。但STL并且不提供Heap集装箱。仅仅提供信息Heap算术运算。只支持RandomAccessIterator该容器可以被用作Heap集装箱。

Heapmax heap和min heap。max heap中每次取出的结点时heap结构中值最大的结点,min heap中每次取出的结点时heap结构中值最小的结点。

在实际应用中。常常常使用vector作为heap容器,heap常常作为priority queue。对于二叉堆。这里有描写叙述http://blog.csdn.net/kangroger/article/details/8718732

当向heap中插入元素时,插入到末尾,“向上维护”就可以:指的是把插入结点与其父结点比較,假设不符合堆得要求则交换,再向上维护其父结点……

当在heap取出元素时,把末尾元素放到Heap头。"向下维护“就可以:指的是父结点与孩子结点比較。假设不满足要求,与较大(较小)一个交换,再维护交换的孩子结点……

Heap不同意遍历其结点,所以Heap没有迭代器。

G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_heap.h 完整列表
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/ /* NOTE: This is an internal header file, included by other STL headers.
* You should not attempt to use it directly.
*/ #ifndef __SGI_STL_INTERNAL_HEAP_H
#define __SGI_STL_INTERNAL_HEAP_H __STL_BEGIN_NAMESPACE #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1209
#endif //向上维护
template <class RandomAccessIterator, class Distance, class T>
void __push_heap(RandomAccessIterator first, Distance holeIndex,
Distance topIndex, T value) {
Distance parent = (holeIndex - 1) / 2; // 找出父结点
//父结点不是heap顶 且 父节点的值小于孩子结点(小于号<能够看出STL维护的是max heap)
while (holeIndex > topIndex && *(first + parent) < value) {
*(first + holeIndex) = *(first + parent); // 父结点下调
holeIndex = parent; // 维护父结点
parent = (holeIndex - 1) / 2;
}
//已达到heap顶或者满足heap性质后,给新插入元素找到合适位置
*(first + holeIndex) = value;
} template <class RandomAccessIterator, class Distance, class T>
inline void __push_heap_aux(RandomAccessIterator first,
RandomAccessIterator last, Distance*, T*) {
__push_heap(first, Distance((last - first) - 1), Distance(0),
T(*(last - 1))); } //往heap加入元素。注意:此函数调用时。元素已经加入到末尾了,且迭代器已经加1了
template <class RandomAccessIterator>
inline void push_heap(RandomAccessIterator first, RandomAccessIterator last) {
// 注意,此函式被呼叫時,新元素應已置於底層容器的最尾端。
__push_heap_aux(first, last, distance_type(first), value_type(first));
} //向上维护,同意用自定义的comp函数,这是未必是max heap了
template <class RandomAccessIterator, class Distance, class T, class Compare>
void __push_heap(RandomAccessIterator first, Distance holeIndex,
Distance topIndex, T value, Compare comp) {
Distance parent = (holeIndex - 1) / 2;//找到父结点的位置
while (holeIndex > topIndex && comp(*(first + parent), value)) {
*(first + holeIndex) = *(first + parent);
holeIndex = parent;
parent = (holeIndex - 1) / 2;
}
*(first + holeIndex) = value;
} template <class RandomAccessIterator, class Compare, class Distance, class T>
inline void __push_heap_aux(RandomAccessIterator first,
RandomAccessIterator last, Compare comp,
Distance*, T*) {
__push_heap(first, Distance((last - first) - 1), Distance(0),
T(*(last - 1)), comp);
} template <class RandomAccessIterator, class Compare>
inline void push_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp) {
__push_heap_aux(first, last, comp, distance_type(first), value_type(first));
} // 下面這個 __adjust_heap() 不允許指定「大小比較標準」
//向下维护heap
template <class RandomAccessIterator, class Distance, class T>
void __adjust_heap(RandomAccessIterator first, Distance holeIndex,
Distance len, T value) {
Distance topIndex = holeIndex;
Distance secondChild = 2 * holeIndex + 2; // 找到右孩子的位置
while (secondChild < len) {//还没到达末尾
// 比較左右孩子大小, secondChild 代表当中较大者
if (*(first + secondChild) < *(first + (secondChild - 1)))
secondChild--;
// 较大的孩子放到父结点位置
*(first + holeIndex) = *(first + secondChild);
holeIndex = secondChild;
// 维护较大的孩子
secondChild = 2 * (secondChild + 1);
}
// 没有右孩子,仅仅有左孩子,仅仅需维护一次,由于左孩子是heap的最后一个元素
if (secondChild == len) {
*(first + holeIndex) = *(first + (secondChild - 1));
holeIndex = secondChild - 1;
} //把调整值放到合适位置,等价于*(first + holeIndex) = value;
__push_heap(first, holeIndex, topIndex, value);
} //向下维护heap
template <class RandomAccessIterator, class T, class Distance>
inline void __pop_heap(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator result, T value, Distance*) {
*result = *first; // 把堆顶的值放到堆尾,堆尾的值保存在參数value中 __adjust_heap(first, Distance(0), Distance(last - first), value); } template <class RandomAccessIterator, class T>
inline void __pop_heap_aux(RandomAccessIterator first,
RandomAccessIterator last, T*) {
__pop_heap(first, last-1, last-1, T(*(last-1)), distance_type(first));
/*
依据implicit representation heap的次序性,pop后的结果是容器的第一个元素。 把最后一个元素放到到容器头,因此维护时维护区间是[first last-1)。 */
}
//取出heap顶元素,依照max heap属性来维护heap
template <class RandomAccessIterator>
inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last) {
__pop_heap_aux(first, last, value_type(first));
} //向下维护heap,同意运行比較函数comp
template <class RandomAccessIterator, class Distance, class T, class Compare>
void __adjust_heap(RandomAccessIterator first, Distance holeIndex,
Distance len, T value, Compare comp) {
Distance topIndex = holeIndex;
Distance secondChild = 2 * holeIndex + 2;
while (secondChild < len) {
if (comp(*(first + secondChild), *(first + (secondChild - 1))))
secondChild--;
*(first + holeIndex) = *(first + secondChild);
holeIndex = secondChild;
secondChild = 2 * (secondChild + 1);
}
if (secondChild == len) {
*(first + holeIndex) = *(first + (secondChild - 1));
holeIndex = secondChild - 1;
}
__push_heap(first, holeIndex, topIndex, value, comp);
} // 取出堆顶元素。同意定义比較函数comp
template <class RandomAccessIterator, class T, class Compare, class Distance>
inline void __pop_heap(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator result, T value, Compare comp,
Distance*) {
*result = *first;
__adjust_heap(first, Distance(0), Distance(last - first), value, comp);
} template <class RandomAccessIterator, class T, class Compare>
inline void __pop_heap_aux(RandomAccessIterator first,
RandomAccessIterator last, T*, Compare comp) {
__pop_heap(first, last - 1, last - 1, T(*(last - 1)), comp,
distance_type(first));
}
//取出heap顶元素,依据comp调整heap
template <class RandomAccessIterator, class Compare>
inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp) {
__pop_heap_aux(first, last, value_type(first), comp);
} template <class RandomAccessIterator, class T, class Distance>
void __make_heap(RandomAccessIterator first, RandomAccessIterator last, T*,
Distance*) {
if (last - first < 2) return;
Distance len = last - first; Distance parent = (len - 2)/2;
//从heap的中间開始向下维护。一次维护[parent, parent-1,……,0]
while (true) { __adjust_heap(first, parent, len, T(*(first + parent)));
if (parent == 0) return;
parent--;
}
} // 将 [first,last) 排列成一个 heap。
template <class RandomAccessIterator>
inline void make_heap(RandomAccessIterator first, RandomAccessIterator last) {
__make_heap(first, last, value_type(first), distance_type(first));
} template <class RandomAccessIterator, class Compare, class T, class Distance>
void __make_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp, T*, Distance*) {
if (last - first < 2) return;
Distance len = last - first;
//从heap的中间開始向下维护。一次维护[parent, parent-1,……,0]
Distance parent = (len - 2)/2; while (true) {
__adjust_heap(first, parent, len, T(*(first + parent)), comp);
if (parent == 0) return;
parent--;
}
}
// 将 [first,last) 排列成一个 heap。同意运行大小比較函数
template <class RandomAccessIterator, class Compare>
inline void make_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp) {
__make_heap(first, last, comp, value_type(first), distance_type(first));
} // 排序(升序)两个迭代器之间的元素,这两个迭代器之间的元素已经满足heap性质了
template <class RandomAccessIterator>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last) { /*
pop_heap()功能是删除heap顶元素,把heap大小减小1,heap顶元素放到末尾。 由此能够看出,sort的结果是升序。
*/
while (last - first > 1)
pop_heap(first, last--); // 每執行 pop_heap() 一次,操作範圍即退縮一格。
} // 排序(升序)两个迭代器之间的元素,这两个迭代器之间的元素已经满足heap性质了。
//同意指定比較函数comp
template <class RandomAccessIterator, class Compare>
void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp) {
while (last - first > 1)
pop_heap(first, last--, comp);
} #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma reset woff 1209
#endif __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_HEAP_H */ // Local Variables:
// mode:C++
// End:

版权声明:本文博主原创文章,博客,未经同意不得转载。

《STL源代码分析》---stl_heap.h读书笔记的更多相关文章

  1. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  2. STL源代码分析——STL算法remove删除算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...

  3. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

  4. STL源代码分析--萃取编程(traits)技术的实现

    1.为什么要出现? 依照默认认定.一个模板给出了一个单一的定义,能够用于用户能够想到的不论什么模板參数!可是对于写模板的人而言,这样的方式并不灵活.特别是遇到模板參数为指针时,若想实现与类型的參量不一 ...

  5. 《Android源代码设计模式解析》读书笔记——Android中你应该知道的设计模式

    断断续续的,<Android源代码设计模式解析>也看了一遍.书中提到了非常多的设计模式.可是有部分在开发中见到的几率非常小,所以掌握不了也没有太大影响. 我认为这本书的最大价值有两点,一个 ...

  6. 《STL源代码分析》---stl_list.h读书笔记

    STL在列表list它是一种经常使用的容器.list不连续双向链表在内存,而且是环形. 理解列表如何操作的详细信息,然后.阅读STL名单上的代码是最好的方法. G++ 2.91.57.cygnus\c ...

  7. 《STL源代码分析》---stl_stack.h读书笔记

    Stack堆栈是频繁使用FILO数据结构,FILO指first in last out,最后出来. 因为只有一个堆叠端口,这也是在口腔进入口. 可以在堆栈中只能操作,你不能访问其它元件的堆叠.器. S ...

  8. STL 源代码分析 算法 stl_heap.h

    本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie heap ----------------------------------------- ...

  9. STL 源代码分析 算法 stl_algo.h -- includes

    本文senlie原,转载请保留此地址:http://blog.csdn.net/zhengsenlie includes(应用于有序区间) ------------------------------ ...

随机推荐

  1. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  2. easyui出口excel无法下载框弹出的办法来解决

    使用前ajax发,码如下面(ActionUrl一般处理程序ashx路径): $.ajax({ url: ActionUrl + '?action=export&ID=' + $('#fm_ID ...

  3. vim ---- 自己主动的按钮indent该命令

    当使用vim一段代码的副本到一个程序时,有,经常indent会有一些问题. . 下面的这个强大的命令,使您可以一键码具有很好的格式. gg=G 样品:         

  4. 解决ubuntu 14.04在显示屏电缆被拔出的问题

    我是一个ubuntu14.04和win7双系统.于win在正常的网络.但在ubuntu网络连接有一直显示线被拔掉,您只能连接到无线Wi-Fi,没有有线网络. 关于这个问题,,最终找到的一种方式,这是进 ...

  5. crm采用soap删除记录

    //抽样 function demo() {     //操作记录id     var targetId = "A8A46444-BA10-E411-8A04-00155D002F02&qu ...

  6. Visual C++学习笔记1:一定要注意ANSI和UNICODE差额

    最近的研究VC++.下载VS2013,根据<Visual C++开发实战系列>首先hello我写了一个常规样品,结果显示乱码编辑框.夜已经折腾型转变.然后总结很明显ANSI和UNICODE ...

  7. 演示基于SDL2.0+FFmpeg的播放器

    SDL是一个跨平台的渲染组件,眼下已经推出到2.0.3版本号,支持Win/Linux/OSX/Android.网上非常多介绍大多是基于SDL1.2版本号的,与2.0版本号有一定的区别,本文演示怎样用S ...

  8. OpenCV在MFC图像控件内显示图像

    1.依照文章<OpenCV+MFC显示图像>,完毕配置. 2.创建对应的图像控件,button控件. 3.进行类型转换. 在当前OpenCV2版本号内,图像格式为cv::Mat ,而该格式 ...

  9. LoaderManager使用具体解释(二)---了解LoaderManager

    了解LoaderManager 这篇文章将介绍LoaderManager类,这是该系列的第二篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四:实例: ...

  10. 基于Velocity开发自己的模板引擎

    Velocity是一个基于java的模板引擎(template engine).它同意不论什么人只简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloc ...