std::rotate vs std::rotate_copy in C++ STL
- rotate in STL:It rotates the order of the elements in the range [first, last), in such a way that the element pointed by middle becomes the new first element, i, e, to the left.
// Illustrating the use of rotate algorithm#include <bits/stdc++.h>usingnamespacestd;// Driver Programintmain(){vector<int> arr;// set some values: 1 2 3 4 5 6// 7 8 9for(inti = 1; i < 10; ++i)arr.push_back(i);// Use of rotaterotate(arr.begin(), arr.begin() + 3, arr.end());// prints the content:cout <<"arr contains:";for(autoi = arr.begin(); i != arr.end(); i++)cout <<' '<< *i;cout << endl;return0;}Output:
arr contains: 4 5 6 7 8 9 1 2 3
- rotate_copy:It copies the elements in the range [first, last) to the range beginning at result, but rotates the order of the elements in such a way that the element pointed by middle becomes the first element in the resulting range, i.e, left rotate.
// Illustrating the use of rotate_copy#include <bits/stdc++.h>usingnamespacestd;// Driver Programintmain(){intarr[] = { 10, 20, 30, 40, 50, 60, 70 };// Use of rotate_copyvector<int> gfg(7);rotate_copy(arr, arr + 3, arr + 7, gfg.begin());// prints the content:cout <<"gfg contains:";for(autoi = gfg.begin(); i != gfg.end(); i++)cout <<' '<< *i;cout << endl;return0;}Output:
gfg contains: 40 50 60 70 10 20 30
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



