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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

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

Интервал:

Закладка:

Сделать

[2] "Equivalent", not "equal", because two equivalent elements (that is, two elements with the property that neither one is less than the other) are not necessarily equal. Operator< is required to induce a strict weak ordering , not necessarily a total ordering . See the LessThan Comparable requirements for a discussion.

See also

lexicographical_compare , equal , mismatch , search , LessThan Comparable, Strict Weak Ordering, sort

next_permutation

Category: algorithms

Component type: function

Prototype

Next_permutation is an overloaded name; there are actually two next_permutation functions.

template

bool next_permutation(BidirectionalIterator first, BidirectionalIterator last);

template

bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, StrictWeakOrdering comp);

Description

Next_permutation transforms the range of elements [first, last) into the lexicographically next greater permutation of the elements. There is a finite number of distinct permutations (at most N! [1], where N is last – first ), so, if the permutations are ordered by lexicographical_compare , there is an unambiguous definition of which permutation is lexicographically next. If such a permutation exists, next_permutation transforms [first, last) into that permutation and returns true . Otherwise it transforms [first, last) into the lexicographically smallest permutation [2] and returns false .

The postcondition is that the new permutation of elements is lexicographically greater than the old (as determined by lexicographical_compare ) if and only if the return value is true .

The two versions of next_permutation 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:

• BidirectionalIterator is a model of Bidirectional Iterator.

• BidirectionalIterator is mutable.

• BidirectionalIterator 's value type is LessThan Comparable.

• The ordering relation on BidirectionalIterator '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:

• BidirectionalIterator is a model of Bidirectional Iterator.

• BidirectionalIterator is mutable.

• StrictWeakOrdering is a model of Strict Weak Ordering.

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

Preconditions

• [first, last) is a valid range.

Complexity

Linear. At most (last – first) / 2 swaps.

Example

This example uses next_permutation to implement the worst known deterministic sorting algorithm. Most sorting algorithms are O(N log(N)) , and even bubble sort is only O(N^2) . This algorithm is actually O(N!) .

template

void snail_sort(BidirectionalIterator first, BidirectionalIterator last) {

while (next_permutation(first, last)) {};

}

int main() {

int A[] = {8, 3, 6, 1, 2, 5, 7, 4};

const int N = sizeof(A) / sizeof(int);

snail_sort(A, A+N);

copy (A, A+N, ostream_iterator(cout, "\n"));

}

Notes

[1] If all of the elements in [first, last) are distinct from each other, then there are exactly N! permutations. If some elements are the same as each other, though, then there are fewer. There are, for example, only three ( 3!/2! ) permutations of the elements 1 1 2 .

[2] Note that the lexicographically smallest permutation is, by definition, sorted in nondecreasing order.

See also

prev_permutation , lexicographical_compare , LessThan Comparable, Strict Weak Ordering, sort

prev_permutation

Category: algorithms

Component type: function

Prototype

Prev_permutation is an overloaded name; there are actually two prev_permutation functions.

template

bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last);

template

bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, StrictWeakOrdering comp);

Description

Prev_permutation transforms the range of elements [first, last) into the lexicographically next smaller permutation of the elements. There is a finite number of distinct permutations (at most N! [1], where N is last – first ), so, if the permutations are ordered by lexicographical_compare , there is an unambiguous definition of which permutation is lexicographically previous. If such a permutation exists, prev_permutation transforms [first, last) into that permutation and returns true . Otherwise it transforms [first, last) into the lexicographically greatest permutation [2] and returns false .

The postcondition is that the new permutation of elements is lexicographically less than the old (as determined by lexicographical_compare ) if and only if the return value is true .

The two versions of prev_permutation 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:

• BidirectionalIterator is a model of Bidirectional Iterator.

• BidirectionalIterator is mutable.

• BidirectionalIterator 's value type is LessThan Comparable.

• The ordering relation on BidirectionalIterator '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:

• BidirectionalIterator is a model of Bidirectional Iterator.

• BidirectionalIterator is mutable.

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

Интервал:

Закладка:

Сделать

Похожие книги на «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