template
<
class
RandomAccessIterator>
// 新元素已插入容器尾部
inline
void
push_heap(RandomAccessIterator first, RandomAccessIterator last) {
__push_heap_aux(first, last, distance_type(first), value_type(first));
}
template
<
class
Iterator>
inline
typename
iterator_traits<Iterator>::difference_type*
distance_type(
const
Iterator&) {
// 决定迭代器difference_type
return
static_cast
<
typename
iterator_traits<Iterator>::difference_type*>(0);
}
template
<
class
Iterator>
inline
typename
iterator_traits<Iterator>::value_type*
value_type(
const
Iterator&) {
// 决定迭代器value_type
return
static_cast
<
typename
iterator_traits<Iterator>::value_type*>(0);
}
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)));
// 新插入元素值
}
template
<
class
RandomAccessIterator,
class
Distance,
class
T>
void
__push_heap(RandomAccessIterator first, Distance holeIndex,
Distance topIndex, T value) {
Distance parent = (holeIndex - 1) / 2;
// 父节点下标
while
(holeIndex > topIndex && *(first + parent) < value) {
// value为新插入元素值
*(first + holeIndex) = *(first + parent);
holeIndex = parent;
// 上移
parent = (holeIndex - 1) / 2;
// 上移
}
*(first + holeIndex) = value;
}
顺序容器(幕后英雄) — heap,码迷,mamicode.com
原文地址:http://blog.csdn.net/nestler/article/details/24768827