Home » C++ algorithm rotate_copy() function

C++ algorithm rotate_copy() function

by Online Tutorials Library

C++ Algorithm rotate_copy()

C++ Algorithm rotate_copy() function is used to make a rotated copy of the elements in the range [first, last).

  • The sequence will start at the element in the middle of the source sequence and the last element will be followed by first.
  • It appends the element between the first and the middle to the elements between the middle and the last element.

Syntax

Parameter

first: A forward iterator pointing the position of the first element in the range to be rotated.

middle: A forward iterator addressing to the element within the range [first, last) that is moved to the first position in the range.

last: A forward iterator pointing the position one past the final element in the range in which the elements are being reversed.

result: An output iterator pointing the position of the first element in the destination range.

Return value

rotate_copy() function returns an output iterator addressing to the end of the copied range.

Complexity

Complexity is linear in the range [first, last): performs an assignment for each element.

Data races

The object in the range [first, last) are accessed.

The object in the range between result and the returned value are changed.

Exceptions

This function throws an exception if either an element assignment or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let’s see the simple example to rotate the given string:

Output:

Before Rotate :  N I K I T A  After Rotate   :  I K I T A N  

Example 2

Let’s see another simple example:

Output:

3 4 5 1 2  

Example 3

Let’s see another simple example:

Output:

Character array s[] : 1. A  2. B  3. C  4. D  5. E  6. F  7. G  8. H    Rotate s[] with 'C' as middle element and copy in t[]  Character array t[] : 1. C  2. D  3. E  4. F  5. G  6. H  7. A  8. B    Rotate t[] with 'A' as middle element and copy in s[]  Character array s[] : 1. A  2. B  3. C  4. D  5. E  6. F  7. G  8. H    Character array t[] : 1. C  2. D  3. E  4. F  5. G  6. H  7. A  8. B    

Example 4

Let’s see another simple example:

Output:

Before calling rotate_copy:    Try this Tongue Twister: she sells sea shells by the sea shore     After calling rotate_copy:    Tongue_Twister: she sells sea shells by the sea shore     Now try the rotated Tongue Twister: shells by the sea shore she sells sea  

Next TopicC++ Algorithm

You may also like