std::rotate vs std::rotate_copy in C++ STL Last Updated : 05 Aug, 2017 Comments Improve Suggest changes Like Article Like Report 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. CPP // Illustrating the use of rotate algorithm #include <bits/stdc++.h> using namespace std; // Driver Program int main() { vector<int> arr; // set some values: 1 2 3 4 5 6 // 7 8 9 for (int i = 1; i < 10; ++i) arr.push_back(i); // Use of rotate rotate(arr.begin(), arr.begin() + 3, arr.end()); // prints the content: cout << "arr contains:"; for (auto i = arr.begin(); i != arr.end(); i++) cout << ' ' << *i; cout << endl; return 0; } 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. CPP // Illustrating the use of rotate_copy #include <bits/stdc++.h> using namespace std; // Driver Program int main() { int arr[] = { 10, 20, 30, 40, 50, 60, 70 }; // Use of rotate_copy vector<int> gfg(7); rotate_copy(arr, arr + 3, arr + 7, gfg.begin()); // prints the content: cout << "gfg contains:"; for (auto i = gfg.begin(); i != gfg.end(); i++) cout << ' ' << *i; cout << endl; return 0; } Output: gfg contains: 40 50 60 70 10 20 30 Comment More infoAdvertise with us Next Article std::rotate vs std::rotate_copy in C++ STL S Shambhavi Singh Improve Article Tags : Technical Scripter C++ STL CPP-Library Practice Tags : CPPSTL Similar Reads std :: reverse_copy in C++ STL C++ STL provides a function that copies the elements from the given range but in reverse order. Below is a simple program to show the working of reverse_copy(). Examples: Input : 1 2 3 4 5 6 7 8 9 10 Output : The vector is: 10 9 8 7 6 5 4 3 2 1 The function takes three parameters. The first two are 2 min read rotate() in C++ STL In C++, rotate() is a built-in function used to rotate the elements of a range in left or right direction such that the element pointed to by a specified iterator becomes the new first element of the range.Let's take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 4 min read strcat() vs strncat() in C++ strcat() The strcat() function will append a copy of the source string to the end of destination string. The strcat() function takes two arguments: 1) dest 2) src It will append copy of the source string in the destination string. The terminating character at the end of dest is replaced by the first 3 min read emplace vs insert in C++ STL In C++, all containers (vector, stack, queue, set, map, etc) support both insert and emplace operations. Both are used to add an element in the container.The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter whi 1 min read copy_n() Function in C++ STL Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size 2 min read Like