std::rotate

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class ForwardIterator >
void rotate( ForwardIterator first, ForwardIterator n_first, ForwardIterator last );

Moves the elements in the range [first, last) in such a way, that the element n_first becomes the first element of the new range and n_first - 1 becomes the last element.

Contents

Parameters

first, last - the range of elements to rotate
n_first - the element to move to the beginning of the new range

Return value

Equivalent function

template<class ForwardIterator>
void rotate(ForwardIterator first, ForwardIterator n_first, ForwardIterator last)
{
    ForwardIterator next = n_first;
    while (first != next) {
        std::swap(*first++, *next++);
        if (next == last) {
            next = n_first;
        } else if (first == n_first) {
            n_first = next;
        }
    }
}

Example

Complexity

linear in the distance between first and last

See also

rotate_copy
copies and rotate a range of elements
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages