Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

pop_heap


Algorithms

Summary

Moves the largest element off the heap.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

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

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

Description

A heap is a particular organization of elements in a range between two random access iterators [a, b). Its two key properties are:

  1. *a is the largest element in the range.

  2. *a may be removed by the pop_heap algorithm or a new element may be added by the push_heap algorithm, in O(logN) time.

These properties make heaps useful as priority queues.

The pop_heap algorithm uses the less than (<) operator as the default comparison. An alternate comparison operator can be specified.

The pop_heap algorithm can be used as part of an operation to remove the largest element from a heap. It assumes that the range [first, last) is a valid heap (in other words, that first is the largest element in the heap or the first element based on the alternate comparison operator). It then swaps the value in the location first with the value in the location last - 1 and makes the range [first, last -1)back into a heap. You can then access the element in last using the vector or deque back() member function, or you can remove the element using the pop_back member function. Note that pop_heap does not actually remove the element from the data structure; you must use another function to do that.

Complexity

pop_heap performs at most 2 * log(last - first) comparisons.

Example

Program Output

Warnings

If your compiler does not support default template parameters, you always need to supply the Allocator template argument. For instance, you need to write:

vector<int, allocator<int> >

instead of:

vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

make_heap, push_heap, sort_heap



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998