Книга: Standard Template Library Programmer

pop_heap

pop_heap

Category: algorithms

Component type: function

Prototype

Pop_heap is an overloaded name; there are actually two pop_heap functions.

template <class RandomAccessIterator>
void pop_heap(RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class StrictWeakOrdering>
inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);

Description

Pop_heap removes the largest element (that is, *first ) from the heap [1] [first, last). The two versions of pop_heap differ in how they define whether one element is less than another. The first version compares objects using operator<, and the second compares objects using a function object comp.

The postcondition for the first version of pop_heap is that is_heap(first, last-1) is true and that *(last – 1) is the element that was removed from the heap. The postcondition for the second version is that is_heap(first, last-1, comp) is true and that *(last – 1) is the element that was removed from the heap. [2]

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version:

• RandomAccessIterator is a model of Random Access Iterator.

• RandomAccessIterator is mutable.

• RandomAccessIterator's value type is a model of LessThan Comparable.

• The ordering on objects of RandomAccessIterator's value type is a strict weak ordering, as defined in the LessThan Comparable requirements.

For the second version:

• RandomAccessIterator is a model of Random Access Iterator.

• RandomAccessIterator is mutable.

• StrictWeakOrdering is a model of Strict Weak Ordering.

• RandomAccessIterator's value type is convertible to StrictWeakOrdering's argument type.

Preconditions

For the first version:

• [first, last) is a valid range.

• [first, last – 1) is a valid range. That is, [first, last) is nonempty.

• [first, last – 1) is a heap. That is, is_heap(first, last – 1) is true.

For the second version:

• [first, last) is a valid range.

• [first, last – 1) is a valid range. That is, [first, last) is nonempty.

• [first, last) is a heap. That is, is_heap(first, last – 1, comp) is true.

Complexity

Logarithmic. At most 2 * log(last – first) comparisons.

Example

int main() {
 int A[] = {1, 2, 3, 4, 5, 6};
 const int N = sizeof(A) / sizeof(int);
 make_heap(A, A+N);
 cout << "Before pop: ";
 copy(A, A+N, ostream_iterator<int>(cout, " "));
 pop_heap(A, A+N);
 cout << endl << "After pop: ";
 copy(A, A+N-1, ostream_iterator<int>(cout, " "));
 cout << endl << "A[N-1] = " << A[N-1] << endl;
}

The output is

Before pop: 6 5 3 4 2 1
After pop: 5 4 3 1 2
A[N-1] = 6

Notes

[1] A heap is a particular way of ordering the elements in a range of Random Access Iterators [f, l). The reason heaps are useful (especially for sorting, or as priority queues) is that they satisfy two important properties. First, *f is the largest element in the heap. Second, it is possible to add an element to a heap (using push_heap), or to remove *f, in logarithmic time. Internally, a heap is a tree represented as a sequential range. The tree is constructed so that that each node is less than or equal to its parent node.

[2] Pop_heap removes the largest element from a heap, and shrinks the heap. This means that if you call keep calling pop_heap until only a single element is left in the heap, you will end up with a sorted range where the heap used to be. This, in fact, is exactly how sort_heap is implemented.

See also

make_heap, push_heap, sort_heap, is_heap, sort

Оглавление книги

Оглавление статьи/книги

Генерация: 2.041. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз