rotate() Function in C++

This C++ program demonstrates the rotate() algorithm. The function rotate() takes three iterators as parameters – first iterator to the beginning of container, second iterator to element by which the elements are rotated and the third iterator to the end of the set of elements to be rotated. The program demonstrates the use of the algorithm which modifies the order of the container whose order is being rotated.

Here is the source code of the C++ program which demonstrates the rotate() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to rotate the order of elements using rotate() algorithm
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. void print(char a[], int N)
  11. {   
  12.     for(int i = 0; i < N; i++)
  13.     {
  14.         cout << (i + 1) << ". " << setw(2)
  15.              << left << a[i] << "  ";
  16.     }
  17.     cout << endl;
  18. }
  19.  
  20. int main()
  21. {
  22.     char s[] = {'A', 'B', 'C', 'D', 'E', 'G', 'H'};
  23.     int slen = sizeof(s) / sizeof(char);
  24.  
  25.     cout << "Original order : ";
  26.     print(s, slen);
  27.     cout << "Rotate with \'C\' as middle element" << endl;
  28.     rotate(s, s + 2, s + slen);
  29.     cout << "Rotated order  : ";
  30.     print(s, slen);
  31.     cout << "Rotate with \'G\' as middle element" << endl;
  32.     rotate(s, s + 3, s + slen);
  33.     cout << "Rotated order  : ";
  34.     print(s, slen);
  35.     cout << "Rotate with \'A\' as middle element" << endl;
  36.     rotate(s, s + 3, s + slen);
  37.     cout << "Original order : ";
  38.     print(s, slen);
  39. }

$ a.out
Original order : 1. A  2. B  3. C  4. D  5. E  6. G  7. H  
Rotate with 'C' as middle element
Rotated order  : 1. C  2. D  3. E  4. G  5. H  6. A  7. B  
Rotate with 'G' as middle element
Rotated order  : 1. G  2. H  3. A  4. B  5. C  6. D  7. E  
Rotate with 'A' as middle element
Original order : 1. B  2. C  3. D  4. E  5. G  6. H  7. A

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.