[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fairly Simple Question about rotate function for vector

Posted on 2004-11-21
2
Medium Priority
?
212 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:pcxboy
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 12638251
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
 
LVL 15

Expert Comment

by:efn
ID: 12638936
         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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question