└── STLsource ├── Algorithm ├── STL算法 │ ├── stl_algo.h │ ├── 合并算法 │ │ └── stl_merge.h │ ├── 排列 │ │ └── stl_permutation.h │ ├── 排序算法 │ │ └── stl_sort.h │ ├── 最大值和最小值 │ │ └── stl_max_min.h │ ├── 查找算法 │ │ └── stl_find.h │ └── 移除算法 │ │ └── stl_remove.h ├── set算法 │ └── set_algo.h ├── 基本算法 │ └── stl_algobase.h └── 数值算法 │ └── stl_numeric.h ├── Allocator ├── 源代码剖析 │ ├── first1.h │ └── second2.h └── 第二级配置器流程图.png ├── Container ├── 关联容器 │ ├── RB-Tree │ │ ├── hea.png │ │ ├── header节点.vsd │ │ └── stl_tree.cpp │ ├── hash_map │ │ └── stl_hash_map.h │ ├── hashtable │ │ ├── stl_hash_fun.h │ │ └── stl_hashtable.cpp │ ├── hashtable_set │ │ └── stl_hash_set.h │ ├── map │ │ └── stl_map.cpp │ ├── multimap │ │ └── stl_multimap.cpp │ ├── multiset │ │ └── stl_multiset.cpp │ ├── pair │ │ └── stl_pair.cpp │ └── set │ │ └── stl_set.cpp └── 序列容器 │ ├── deque │ ├── stl_deque.cpp │ ├── 三者关系.png │ ├── 中控器map.png │ ├── 容器结构.png │ ├── 绘图1.vsd │ └── 迭代器.png │ ├── heap │ └── stl_heap.cpp │ ├── list │ ├── erase.vsd │ ├── insert_and_node.vsd │ ├── sort.vsd │ ├── sort │ │ ├── sort执行过程.vsd │ │ ├── 待排序原始链表.png │ │ ├── 最后结果.png │ │ ├── 第一次while循环.png │ │ ├── 第三次while循环.png │ │ ├── 第二次while循环.png │ │ └── 第四次while循环.png │ ├── stl_list.cpp │ ├── transfer.vsd │ ├── transfer2.vsd │ ├── transfer第一过程.png │ ├── transfer第二过程.png │ ├── 删除过程.png │ ├── 插入过程.png │ └── 数据结构.png │ ├── queue │ └── stl_queue.cpp │ ├── slist │ └── stl_slist.cpp │ ├── stack │ └── stl_stack.cpp │ └── vector │ ├── case2.png │ ├── erase擦除过程.png │ ├── stl_vector.cpp │ ├── stl_vector.h │ ├── vector结构图.png │ ├── 插入case1-a.png │ ├── 插入case1-a.vsd │ ├── 插入case1-b.vsd │ ├── 插入case2.vsd │ ├── 插入过程case1-b.png │ ├── 擦除过程.vsd │ ├── 源1.cpp │ └── 绘图3.vsd ├── FunctionObject └── stl_function.h ├── Iterator └── __type_traits.cpp ├── README.md └── pool ├── stl_construct.cpp └── stl_uninitialized.cpp /STLsource/Algorithm/STL算法/stl_algo.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/stl_algo.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/合并算法/stl_merge.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/合并算法/stl_merge.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/排列/stl_permutation.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/排列/stl_permutation.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/排序算法/stl_sort.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/排序算法/stl_sort.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/最大值和最小值/stl_max_min.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/最大值和最小值/stl_max_min.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/查找算法/stl_find.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/查找算法/stl_find.h -------------------------------------------------------------------------------- /STLsource/Algorithm/STL算法/移除算法/stl_remove.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/STL算法/移除算法/stl_remove.h -------------------------------------------------------------------------------- /STLsource/Algorithm/set算法/set_algo.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/set算法/set_algo.h -------------------------------------------------------------------------------- /STLsource/Algorithm/基本算法/stl_algobase.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/基本算法/stl_algobase.h -------------------------------------------------------------------------------- /STLsource/Algorithm/数值算法/stl_numeric.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Algorithm/数值算法/stl_numeric.h -------------------------------------------------------------------------------- /STLsource/Allocator/源代码剖析/first1.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Allocator/源代码剖析/first1.h -------------------------------------------------------------------------------- /STLsource/Allocator/源代码剖析/second2.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Allocator/源代码剖析/second2.h -------------------------------------------------------------------------------- /STLsource/Allocator/第二级配置器流程图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Allocator/第二级配置器流程图.png -------------------------------------------------------------------------------- /STLsource/Container/关联容器/RB-Tree/hea.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/RB-Tree/hea.png -------------------------------------------------------------------------------- /STLsource/Container/关联容器/RB-Tree/header节点.vsd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/RB-Tree/header节点.vsd -------------------------------------------------------------------------------- /STLsource/Container/关联容器/RB-Tree/stl_tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/RB-Tree/stl_tree.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/hash_map/stl_hash_map.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/hash_map/stl_hash_map.h -------------------------------------------------------------------------------- /STLsource/Container/关联容器/hashtable/stl_hash_fun.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/hashtable/stl_hash_fun.h -------------------------------------------------------------------------------- /STLsource/Container/关联容器/hashtable/stl_hashtable.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/hashtable/stl_hashtable.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/hashtable_set/stl_hash_set.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/hashtable_set/stl_hash_set.h -------------------------------------------------------------------------------- /STLsource/Container/关联容器/map/stl_map.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/map/stl_map.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/multimap/stl_multimap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/multimap/stl_multimap.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/multiset/stl_multiset.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/multiset/stl_multiset.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/pair/stl_pair.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/pair/stl_pair.cpp -------------------------------------------------------------------------------- /STLsource/Container/关联容器/set/stl_set.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/关联容器/set/stl_set.cpp -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/stl_deque.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/stl_deque.cpp -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/三者关系.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/三者关系.png -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/中控器map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/中控器map.png -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/容器结构.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/容器结构.png -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/绘图1.vsd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/绘图1.vsd -------------------------------------------------------------------------------- /STLsource/Container/序列容器/deque/迭代器.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenhanzhun/STL/f1dce2878395f2a6fdb39667a8db9c38b030cf5a/STLsource/Container/序列容器/deque/迭代器.png -------------------------------------------------------------------------------- /STLsource/Container/序列容器/heap/stl_heap.cpp: -------------------------------------------------------------------------------- 1 | //在STL中,heap不作为容器,只是提供了关于Heap操作的算法。 2 | //heap没有自己的迭代器,只要支持RandomAccessIterator的容器都可以作为Heap容器 3 | #ifndef __SGI_STL_INTERNAL_HEAP_H 4 | #define __SGI_STL_INTERNAL_HEAP_H 5 | 6 | __STL_BEGIN_NAMESPACE 7 | 8 | #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 9 | #pragma set woff 1209 10 | #endif 11 | 12 | // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap. 13 | 14 | //堆中添加元素;关于push_heap操作的原型有两个 15 | //注意: push_heap()操作之前必须保证新添加的元素已经加入到容器末尾 16 | //*************************************************************** 17 | //* 第一个版本使用operator<操作 18 | //* template< class RandomIt > 19 | //* void push_heap( RandomIt first, RandomIt last ); 20 | //*************************************************************** 21 | //* 第二个版本使用比较函数comp 22 | //* template< class RandomIt, class Compare > 23 | //* void push_heap( RandomIt first, RandomIt last,Compare comp ); 24 | //*************************************************************** 25 | //* 比较函数comp:comparison function which returns ​true if the first argument is less than the second. 26 | //* The signature of the comparison function should be equivalent to the following: 27 | //* bool cmp(const Type1 &a, const Type2 &b); 28 | //*************************************************************** 29 | template 30 | void 31 | __push_heap(_RandomAccessIterator __first, 32 | _Distance __holeIndex, _Distance __topIndex, _Tp __value) 33 | {//当前节点标号为__holeIndex- 1即为新插入元素标号,因为根节点标号是从0开始,所以这里要-1 34 | _Distance __parent = (__holeIndex - 1) / 2;//找出当前节点的父节点 35 | //尚未达到根节点,且所插入数据value大于父节点的关键字值 36 | while (__holeIndex > __topIndex && *(__first + __parent) < __value) { 37 | *(__first + __holeIndex) = *(__first + __parent);//交换当前节点元素与其父节点元素的值 38 | __holeIndex = __parent;//更新当前节点标号,上溯 39 | __parent = (__holeIndex - 1) / 2;//更新父节点 40 | } //持续达到根节点,或满足heap的性质 41 | *(__first + __holeIndex) = __value;//插入正确的位置 42 | } 43 | 44 | template 45 | inline void 46 | __push_heap_aux(_RandomAccessIterator __first, 47 | _RandomAccessIterator __last, _Distance*, _Tp*) 48 | { 49 | //__last - __first) - 1表示插入后元素的个数,也是容器的最后一个下标数字 50 | //新插入的元素必须位于容器的末尾 51 | __push_heap(__first, _Distance((__last - __first) - 1), _Distance(0), 52 | _Tp(*(__last - 1))); 53 | } 54 | 55 | //第一个版本push_heap默认是operator<操作 56 | template 57 | inline void 58 | push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 59 | { 60 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 61 | __STL_REQUIRES(typename iterator_traits<_RandomAccessIterator>::value_type, 62 | _LessThanComparable); 63 | __push_heap_aux(__first, __last, 64 | __DISTANCE_TYPE(__first), __VALUE_TYPE(__first)); 65 | } 66 | 67 | template 69 | void 70 | __push_heap(_RandomAccessIterator __first, _Distance __holeIndex, 71 | _Distance __topIndex, _Tp __value, _Compare __comp) 72 | { 73 | _Distance __parent = (__holeIndex - 1) / 2; 74 | while (__holeIndex > __topIndex && __comp(*(__first + __parent), __value)) { 75 | *(__first + __holeIndex) = *(__first + __parent); 76 | __holeIndex = __parent; 77 | __parent = (__holeIndex - 1) / 2; 78 | } 79 | *(__first + __holeIndex) = __value; 80 | } 81 | 82 | template 84 | inline void 85 | __push_heap_aux(_RandomAccessIterator __first, 86 | _RandomAccessIterator __last, _Compare __comp, 87 | _Distance*, _Tp*) 88 | { 89 | __push_heap(__first, _Distance((__last - __first) - 1), _Distance(0), 90 | _Tp(*(__last - 1)), __comp); 91 | } 92 | //第二个版本push_heap自定义比较操作函数comp 93 | template 94 | inline void 95 | push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, 96 | _Compare __comp) 97 | { 98 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 99 | __push_heap_aux(__first, __last, __comp, 100 | __DISTANCE_TYPE(__first), __VALUE_TYPE(__first)); 101 | } 102 | 103 | //注意: pop_heap()操作, 执行完操作后要自己将容器尾元素弹出 104 | //default (1): 105 | // template 106 | // void pop_heap (RandomAccessIterator first, RandomAccessIterator last); 107 | //custom (2): 108 | // template 109 | // void pop_heap (RandomAccessIterator first, RandomAccessIterator last, 110 | // Compare comp); 111 | //*********************************************************************** 112 | template 113 | void 114 | __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex, 115 | _Distance __len, _Tp __value) 116 | { 117 | _Distance __topIndex = __holeIndex;//根节点标号 118 | _Distance __secondChild = 2 * __holeIndex + 2;//获取子节点 119 | while (__secondChild < __len) {//若子节点标号比总的标号数小 120 | if (*(__first + __secondChild) < *(__first + (__secondChild - 1))) 121 | __secondChild--;//找出堆中最大关键字值的节点 122 | //若堆中存在比新根节点元素(即原始堆最后节点关键字值)大的节点,则交换位置 123 | *(__first + __holeIndex) = *(__first + __secondChild); 124 | __holeIndex = __secondChild;//更新父节点 125 | __secondChild = 2 * (__secondChild + 1);//更新子节点 126 | } 127 | if (__secondChild == __len) { 128 | *(__first + __holeIndex) = *(__first + (__secondChild - 1)); 129 | __holeIndex = __secondChild - 1; 130 | } 131 | __push_heap(__first, __holeIndex, __topIndex, __value); 132 | } 133 | 134 | template 135 | inline void 136 | __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, 137 | _RandomAccessIterator __result, _Tp __value, _Distance*) 138 | { 139 | *__result = *__first;//把原始堆的根节点元素放在容器的末尾 140 | //调整剩下的节点元素,使其成为新的heap 141 | __adjust_heap(__first, _Distance(0), _Distance(__last - __first), __value); 142 | } 143 | 144 | template 145 | inline void 146 | __pop_heap_aux(_RandomAccessIterator __first, _RandomAccessIterator __last, 147 | _Tp*) 148 | { 149 | __pop_heap(__first, __last - 1, __last - 1, 150 | _Tp(*(__last - 1)), __DISTANCE_TYPE(__first)); 151 | } 152 | 153 | template 154 | inline void pop_heap(_RandomAccessIterator __first, 155 | _RandomAccessIterator __last) 156 | { 157 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 158 | __STL_REQUIRES(typename iterator_traits<_RandomAccessIterator>::value_type, 159 | _LessThanComparable); 160 | __pop_heap_aux(__first, __last, __VALUE_TYPE(__first)); 161 | } 162 | 163 | template 165 | void 166 | __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex, 167 | _Distance __len, _Tp __value, _Compare __comp) 168 | { 169 | _Distance __topIndex = __holeIndex; 170 | _Distance __secondChild = 2 * __holeIndex + 2; 171 | while (__secondChild < __len) { 172 | if (__comp(*(__first + __secondChild), *(__first + (__secondChild - 1)))) 173 | __secondChild--; 174 | *(__first + __holeIndex) = *(__first + __secondChild); 175 | __holeIndex = __secondChild; 176 | __secondChild = 2 * (__secondChild + 1); 177 | } 178 | if (__secondChild == __len) { 179 | *(__first + __holeIndex) = *(__first + (__secondChild - 1)); 180 | __holeIndex = __secondChild - 1; 181 | } 182 | __push_heap(__first, __holeIndex, __topIndex, __value, __comp); 183 | } 184 | 185 | template 187 | inline void 188 | __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, 189 | _RandomAccessIterator __result, _Tp __value, _Compare __comp, 190 | _Distance*) 191 | { 192 | *__result = *__first; 193 | __adjust_heap(__first, _Distance(0), _Distance(__last - __first), 194 | __value, __comp); 195 | } 196 | 197 | template 198 | inline void 199 | __pop_heap_aux(_RandomAccessIterator __first, 200 | _RandomAccessIterator __last, _Tp*, _Compare __comp) 201 | { 202 | __pop_heap(__first, __last - 1, __last - 1, _Tp(*(__last - 1)), __comp, 203 | __DISTANCE_TYPE(__first)); 204 | } 205 | 206 | template 207 | inline void 208 | pop_heap(_RandomAccessIterator __first, 209 | _RandomAccessIterator __last, _Compare __comp) 210 | { 211 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 212 | __pop_heap_aux(__first, __last, __VALUE_TYPE(__first), __comp); 213 | } 214 | 215 | 216 | //创建堆 217 | //default(1): 218 | // template 219 | // void make_heap (RandomAccessIterator first, RandomAccessIterator last); 220 | //custom (2): 221 | // template 222 | // void make_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp ); 223 | //******************************************************************************** 224 | template 225 | void 226 | __make_heap(_RandomAccessIterator __first, 227 | _RandomAccessIterator __last, _Tp*, _Distance*) 228 | { 229 | if (__last - __first < 2) return; 230 | _Distance __len = __last - __first; 231 | _Distance __parent = (__len - 2)/2; 232 | 233 | while (true) { 234 | __adjust_heap(__first, __parent, __len, _Tp(*(__first + __parent))); 235 | if (__parent == 0) return; 236 | __parent--; 237 | } 238 | } 239 | 240 | template 241 | inline void 242 | make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 243 | { 244 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 245 | __STL_REQUIRES(typename iterator_traits<_RandomAccessIterator>::value_type, 246 | _LessThanComparable); 247 | __make_heap(__first, __last, 248 | __VALUE_TYPE(__first), __DISTANCE_TYPE(__first)); 249 | } 250 | 251 | template 253 | void 254 | __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, 255 | _Compare __comp, _Tp*, _Distance*) 256 | { 257 | if (__last - __first < 2) return; 258 | _Distance __len = __last - __first; 259 | _Distance __parent = (__len - 2)/2; 260 | 261 | while (true) { 262 | __adjust_heap(__first, __parent, __len, _Tp(*(__first + __parent)), 263 | __comp); 264 | if (__parent == 0) return; 265 | __parent--; 266 | } 267 | } 268 | 269 | template 270 | inline void 271 | make_heap(_RandomAccessIterator __first, 272 | _RandomAccessIterator __last, _Compare __comp) 273 | { 274 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 275 | __make_heap(__first, __last, __comp, 276 | __VALUE_TYPE(__first), __DISTANCE_TYPE(__first)); 277 | } 278 | 279 | //排序堆里面的内容 280 | //default(1): 281 | // template 282 | // void sort_heap (RandomAccessIterator first, RandomAccessIterator last); 283 | //custom (2): 284 | // template 285 | // void sort_heap (RandomAccessIterator first, RandomAccessIterator last, 286 | // Compare comp); 287 | //************************************************************************** 288 | template 289 | void sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 290 | { 291 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 292 | __STL_REQUIRES(typename iterator_traits<_RandomAccessIterator>::value_type, 293 | _LessThanComparable); 294 | while (__last - __first > 1) 295 | pop_heap(__first, __last--);//每次取出根节点元素,直到heap为空 296 | } 297 | 298 | template 299 | void 300 | sort_heap(_RandomAccessIterator __first, 301 | _RandomAccessIterator __last, _Compare __comp) 302 | { 303 | __STL_REQUIRES(_RandomAccessIterator, _Mutable_RandomAccessIterator); 304 | while (__last - __first > 1) 305 | pop_heap(__first, __last--, __comp); 306 | } 307 | 308 | #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 309 | #pragma reset woff 1209 310 | #endif 311 | 312 | __STL_END_NAMESPACE 313 | 314 | #endif /* __SGI_STL_INTERNAL_HEAP_H */ 315 | 316 | // Local Variables: 317 | // mode:C++ 318 | // End: 319 | // range heap example 320 | 321 | 322 | #include // std::cout 323 | #include // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap 324 | #include // std::vector 325 | 326 | int main () { 327 | int myints[] = {10,20,30,5,15}; 328 | std::vector v(myints,myints+5); 329 | 330 | std::make_heap (v.begin(),v.end()); 331 | std::cout << "initial max heap : " << v.front() << '\n'; 332 | 333 | std::pop_heap (v.begin(),v.end()); v.pop_back(); 334 | std::cout << "max heap after pop : " << v.front() << '\n'; 335 | 336 | v.push_back(99); std::push_heap (v.begin(),v.end()); 337 | std::cout << "max heap after push: " << v.front() << '\n'; 338 | 339 | std::sort_heap (v.begin(),v.end()); 340 | 341 | std::cout << "final sorted range :"; 342 | for (unsigned i=0; i