Page 1 of 1

Using the STL algorithm::rotate() Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 19 May 2008 - 04:08 AM

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

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:
#include <iostream>
#include <algorithm>
#include <vector>

Now, what are we going to use from the std namespace? We need:
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 :)

First off, we're going to need a vector and an iterator:
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:
// 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.
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:
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)
#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
}


Is This A Good Question/Topic? 0
  • +

Replies To: Using the STL algorithm::rotate()

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Posted 27 May 2008 - 07:19 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1