Fairly Simple Question about rotate function for vector

Hello. I want to create a function that will take a vector that has these elements:

1,2,3,4,5

In that order, and rotate them about a specific point...say 3, so that the vector's contents would look like this:

3,4,5,1,2

I have found an incredibly easy method to do it with the vector.at(), however, I would like to do it using iterators. Here is my code, however the vector isn't rotated. My question is, what is going wrong with my code?


template < typename t_iterator >
void rotate_around( t_iterator &start, t_iterator &middle, t_iterator &end )
{
      unsigned int c=0;
      while(middle!=end)
      {
            for( t_iterator i = middle; i!=(start+c); i-- )
            {
                  std::swap( *middle, *(middle-1) );
            }
            middle++;
            c++;
      }
}

Thanks in advance.
pcxboyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
I think must be something like this, please check and advice

template < typename t_iterator >
void rotate_around( t_iterator &start, t_iterator &middle, t_iterator &end )
{
     while(middle!=end)
     {
          std::swap(*middle, *start);
          middle++;
          start++;
     }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
efnCommented:
         for( t_iterator i = middle; i!=(start+c); i-- )
          {
               std::swap( *middle, *(middle-1) );
          }

This swaps the same two elements over and over and over.  If the number of times it iterates is odd, it will have the effect of one swap.  If the number is even, it will have no effect whatsoever.  This is probably not what you intended.

Since the variable i is what's changing on every iteration, maybe you want to do something with i in the loop body.

--efn
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.