Книга: Standard Template Library Programmer
make_heap
Разделы на этой странице:
make_heap
Category: algorithms
Component type: function
Prototype
Make_heap is an overloaded name; there are actually two make_heap functions.
template <class RandomAccessIterator>
void make_heap(RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class StrictWeakOrdering>
void make_heap(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);
Description
Make_heap turns the range [first, last) into a heap [1].
The two versions of make_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. In the first version the postcondition is that is_heap(first, last) is true, and in the second version the postcondition is that is_heap(first, last, comp) is true.
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
• [first, last) is a valid range.
Complexity
Linear. At most 3*(last – first) comparisons.
Example
int main() {
int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);
make_heap(A, A+N);
copy(A, A+N, ostream_iterator<int>(cout, " "));
cout << endl;
sort_heap (A, A+N);
copy(A, A+N, ostream_iterator <int>(cout, " "));
cout << endl;
}
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 simply 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.
See also
push_heap , pop_heap , sort_heap , sort , is_heap
- sort_heap
- push_heap
- Heap operations
- pop_heap
- is_heap
- 4.3.3. Makefile Targets
- Scaling makes your object darker?
- 1.3. Автоматизация процесса с помощью GNU-утилиты make
- 15.4. Утилита make: автоматизация процедур
- Команда make и make-файлы
- 5.1.1 Make Targets
- The Master Algorithm: How the Quest for the Ultimate Learning Machine Will Remake Our World