Standard Template Library Programmer's Guide

Здесь есть возможность читать онлайн «Standard Template Library Programmer's Guide» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, Справочники, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Standard Template Library Programmer's Guide: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Standard Template Library Programmer's Guide»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

This document contains reference on SGI STL implementation

Standard Template Library Programmer's Guide — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Standard Template Library Programmer's Guide», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать
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(cout, " "));

pop_heap(A, A+N);

cout << endl << "After pop: ";

copy(A, A+N-1, ostream_iterator(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

make_heap

Category: algorithms

Component type: function

Prototype

Make_heap is an overloaded name; there are actually two make_heap functions.

template

void make_heap(RandomAccessIterator first, RandomAccessIterator last);

template

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(cout, " "));

cout << endl;

sort_heap (A, A+N);

copy(A, A+N, ostream_iterator (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

Category: algorithms

Component type: function

Prototype

Sort_heap is an overloaded name; there are actually two sort_heap functions.

template

void sort_heap(RandomAccessIterator first, RandomAccessIterator last);

template

void sort_heap(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);

Description

Sort_heap turns a heap [1] [first, last) into a sorted range. Note that this is not a stable sort: the relative order of equivalent elements is not guaranteed to be preserved.

The two versions of sort_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 .

Definition

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

Requirements on types

For the first version, the one that takes two arguments:

• 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, the one that takes three arguments:

• 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.

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Standard Template Library Programmer's Guide»

Представляем Вашему вниманию похожие книги на «Standard Template Library Programmer's Guide» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «Standard Template Library Programmer's Guide»

Обсуждение, отзывы о книге «Standard Template Library Programmer's Guide» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x