- template <class RandomAccessIterator>
- inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last) {
- __pop_heap_aux(first, last, value_type(first));
- }
- 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));
- }
- template <class RandomAccessIterator, class T, class Distance>
- inline void __pop_heap(RandomAccessIterator first,
- RandomAccessIterator last,
- RandomAccessIterator result,
- T value, Distance*) {
- *result = *first;
- __adjust_heap(first, Distance(0), Distance(last - first), value);
- }
- template <class RandomAccessIterator, class Distance, class T>
- void __adjust_heap(RandomAccessIterator first, Distance holeIndex,
- Distance len, T vaule) {
- Distance topIndex = holeIndex;
- Distance secondChild = holeIndex*2 + 2;
- while(secondChild < len) {
- if(*(first + secondChild) < *(first + secondChild - 1))
- --secondChild;
- *(first + holeIndex) = *(first + secondChild);
- holeIndex = secondChild;
- secondChild = holeIndex*2 + 2;
- }
- if(secondChild == len) {
- *(first + holeIndex) = *(first + secondChild - 1);
- holeIndex = secondChild - 1;
- }
- __push_heap(first, holeIndex, topIndex, vaule);
- }
注:__push_heap的源码请见 http://zxn990.blog.51cto.com/4253193/1165513
转载于:https://blog.51cto.com/zxn990/1165524