stlab.adobe.com Adobe Systems Incorporated

rotate

algorithms.gif
function.gif
Category: algorithms Component type: function

Prototype

template <class ForwardIterator>
inline ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
                   ForwardIterator last);

Description

Rotate rotates the elements in a range. That is, the element pointed to by 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 ForwardIterator.
  • 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\n", 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 ForwardIterator, BidirectionalIterator, or RandomAccessIterator. All three algorithms, however, are linear.

See also

rotate_copy

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google