Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,772 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,472 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Using the STL algorithm::rotate()

 
Reply to this topicStart new topic

> Using the STL algorithm::rotate()

gabehabe
Group Icon



post 19 May, 2008 - 04:08 AM
Post #1


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
}
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Amadeus
Group Icon



post 27 May, 2008 - 07:19 AM
Post #2
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.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/21/08 03:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month