rotate_copy() Function in C++

This C++ program demonstrates the rotate_copy() algorithm. The function rotate_copy() takes four iterators as parameters – first iterator to the beginning of container, second iterator to element by which the elements are rotated, third iterator to the end of the set of elements to be rotated and the fourth iterator to the starting of the container where rotated elements are to be stored. The program demonstrates the use of the algorithm which doesn’t modify the order of the container whose order is being rotated.

Here is the source code of the C++ program which demonstrates the rotate_copy() 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_copy() 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(1)
  15.              << left << a[i] << "  ";
  16.     }
  17.     cout << endl;
  18. }
  19.  
  20. int main()
  21. {
  22.     char s[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  23.     int slen = sizeof(s) / sizeof(char), tlen = slen;
  24.     char t[tlen];
  25.  
  26.     cout << "Character array s[] : ";
  27.     print(s, slen);
  28.     cout << "Rotate s[] with \'C\' as middle element and copy in t[]" << endl;
  29.     rotate_copy(s, s + 2, s + slen, t);
  30.     cout << "Character array t[] : ";
  31.     print(t, tlen);
  32.     cout << "Rotate t[] with \'A\' as middle element and copy in s[]" << endl;
  33.     rotate_copy(t, t + 6, t + tlen, s);
  34.     cout << "Character array s[] : ";
  35.     print(s, slen);
  36.     cout << "Character array t[] : ";
  37.     print(t, tlen);
  38. }

$ a.out
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

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

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.