middle is moved to the position first, the element pointed to by middle + 1 is moved to the position first + 1, and so on. One way to think about this operation is that it exchanges the two ranges [first, middle) and [middle, last). Formally, for every integer n such that 0 <= n < last – first, the element *(first + n) is assigned to *(first + (n + (last – middle)) % (last – first)). Rotate returns first + (last – middle).

Definition

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

Requirements on types

• ForwardIterator is a model of Forward Iterator.

• ForwardIterator is mutable.

Preconditions

• [first, middle) is a valid range.

• [middle, last) is a valid range. [1]

Complexity

Linear. At most last – first swaps are performed. [2]

Example

char alpha[] = 'abcdefghijklmnopqrstuvwxyz';

rotate(alpha, alpha + 13, alpha + 26);

printf('%s ', alpha);

// The output is nopqrstuvwxyzabcdefghijklm

Notes

[1] It follows from these two requirements that [first, last) is a valid range.

[2] Rotate uses a different algorithm depending on whether its arguments are Forward Iterators, Bidirectional Iterators, or Random Access Iterators. All three algorithms, however, are linear.

See also

rotate_copy

rotate_copy

Category: algorithms

Component type: function

Prototype

template <class ForwardIterator, class OutputIterator>

OutputIterator rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);

Description

Rotate_copy copies elements from the range [first, last) to the range [result, result + (last – first)) such that *middle is copied to *result, *(middle + 1) is copied to *(result + 1), and so on. Formally, for every integer n such that 0 <= n < last – first, rotate_copy performs the assignment *(result + (n + (last – middle)) % (last – first)) = *(first + n). Rotate_copy is similar to copy followed by rotate, but is more efficient. The return value is result + (last – first).

Definition

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

Requirements on types

• ForwardIterator is a model of Forward Iterator.

• OutputIterator is a model of Output Iterator.

• ForwardIterator's value type is convertible to a type in OutputIterator's set of value types.

Preconditions

• [first, middle) is a valid range.

• [middle, last) is a valid range. [1]

• There is enough space to hold all of the elements being copied. More formally, the requirement is that [result, result + (last – first)) is a valid range.

• The ranges [first, last) and [result, result + (last – first)) do not overlap.

Complexity

Linear. Rotate_copy performs exactly last – first assignments.

Example

const char alpha[] = 'abcdefghijklmnopqrstuvwxyz';

rotate_copy(alpha, alpha + 13, alpha + 26, ostream_iterator<char>(cout));

// The output is nopqrstuvwxyzabcdefghijklm

Notes

[1] It follows from these two requirements that [first, last) is a valid range.

See also

rotate, copy.

random_shuffle

Category: algorithms

Component type: function

Prototype

Random_shuffle is an overloaded name; there are actually two random_shuffle functions.

template <class RandomAccessIterator>

void random_shuffle(RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand)

Description

Random_shuffle randomly rearranges the elements in the range [first, last): that is, it randomly picks one of the N! possible orderings, where N is last – first. [1] There are two different versions of random_shuffle. The first version uses an internal random number generator, and the second uses a Random Number Generator, a special kind of function object , that is explicitly passed as an argument.

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату