Full Version: Using the STL algorithm::rotate()
Dream.In.Code > Programming Tutorials > C++ Tutorials
gabehabe
What do I need to know before studying this lesson?
You should have a knowledge of vectors and vector iterators

What does rotate do?
Rotate will shift the values of a vector to the left or right. Example output:
QUOTE("rotate algorithm")
before rotation: 1 2 3 4 5 6 7 8 9
after rotation: 6 7 8 9 1 2 3 4 5


Before we get started, here is the general syntax for the rotate function:
rotate (<vector position to start>, <how many values to shift across>, <where to end>);
DON'T BE INTIMIDATED BY THIS, IT REALLY IS SIMPLE!

So how do we start?
What do we need to include? Well, we need a vector, an algorithm and some I/O (to see what's going on) So:
cpp
#include <iostream>
#include <algorithm>
#include <vector>

Now, what are we going to use from the std namespace? We need:
cpp
using std::cout; // output the values to see what's happening
using std::endl; // make our output easier to read
using std::cin; // pause for input
using std::vector; // and of course, we need a vector

And then it's on to our int main() function smile.gif

First off, we're going to need a vector and an iterator:
cpp
vector <int> myVec; // create a vector called myVec
vector <int> :: iterator it; // create a vector iterator called it

And then we simply need to fill our vector with some values, and output what we have so far:
cpp
// fill myVec with some values
for (int i = 1; i < 10; i++)
myVec.push_back (i);

// output the values before rotation
cout << "Before rotation: ";
for (it = myVec.begin(); it < myVec.end(); ++it)
cout << *it << " ";

Simple enough, right? Now it's time to use the rotate function, to shift our values.
cpp
rotate (myVec.begin(), myVec.begin()+5, myVec.end());

So, this will begin the rotation at the beginning of the vector, shift it by 5 values, and stop when it reaches the end of the vector.

Now, let's output the contents of our vector, using our iterator:
cpp
cout << endl; // add a line between our previous output

cout << "After rotation: ";
for (it = myVec.begin(); it < myVec.end(); ++it)
cout << *it;

And that's all there is to it!
Here is the final code, commented: (some people can learn better from just the code)
cpp
#include <iostream> // standard I/O
#include <algorithm> // we will use the rotate algorithm
#include <vector> // we need a vector to rotate!

using std::cout; // output the values to see what's happening
using std::endl; // make our output easier to read
using std::cin; // pause for input
using std::vector; // and of course, we need a vector


int main () // beginning of main
{
vector <int> myVec; // create a vector of ints called myVec
vector <int>::iterator it; // create a vector iterator called it

// now let's fill our vector with some values
for (int i = 1; i < 10; i++) // 9 loops
myVec.push_back (i); // add the value of i to our vector

cout << "Before rotation: "; // before rotation (simple enough!)
for (it = myVec.begin(); it < myVec.end(); ++it) // use it to iterate through myVec
cout << *it << " "; // output it

// now it's time for rotate
// we're going to start at the beginning - myVec.begin()
// we're going to shift the values across by 5 - myVec.begin()+5
// and we're going to stop when we reach the end of the vector - myVec.end()
rotate (myVec.begin(), myVec.begin()+5,myVec.end());

cout << endl; // add an empty line for our next output

cout << "After rotation: "; // after rotation
for (it = myVec.begin(); it < myVec.end(); ++it) // use it to iterate through myVec
cout << *it << " "; // output it

cin.get (); // pause for input
return EXIT_SUCCESS; // everything went OK
}
Amadeus
This tutorial has been approved, as the information is factually correct. This, with other submissions, however, are really examples of some of the basic features of the algorithm header. Perhaps a more encompassing tutorials grouping several methods together might be beneficial.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.