cycling through a vector?

J

JoeC

I have a vector of elements, how can I take the first element of a
vector and put it in the end and have all the other elements cycle
down? Ex:
1, 2, 3, 4...
cycle
4, 1, 2 ,3
 
K

Kai-Uwe Bux

JoeC said:
I have a vector of elements, how can I take the first element of a
vector and put it in the end and have all the other elements cycle
down? Ex:
1, 2, 3, 4...
cycle
4, 1, 2 ,3

Your example does not match your description. The following goes with the
specification and not with the example.

Use std::deque instead: std::deque supports efficient insertions and
deletions on either end. Then you can do in constant time:

cont.push_back( cont.front() );
cont.pop_front();

If you need to stay with vector, there is only a linear time solution. In
that case, you could as well go with std::rotate() from <algorithm>:

std::rotate( cont.begin(), cont.begin()+1, cont.end() );


Best

Kai-Uwe Bux
 
M

Mark P

JoeC said:
I have a vector of elements, how can I take the first element of a
vector and put it in the end and have all the other elements cycle
down? Ex:
1, 2, 3, 4...
cycle
4, 1, 2 ,3

if( !my_vector.empty())
{
int val = my_vector.front();
my_vector.erase( my_vector.begin());
my_vector.push_back( val);
}

It's not a very efficient operation though. The entire sequence has to
be copied in order to move it down by 1. If your vector is small then
it doesn't matter much, otherwise you should consider a different
design. A std::deque might be appropriate.
 
J

JoeC

Kai-Uwe Bux said:
Your example does not match your description. The following goes with the
specification and not with the example.

Use std::deque instead: std::deque supports efficient insertions and
deletions on either end. Then you can do in constant time:

cont.push_back( cont.front() );
cont.pop_front();

If you need to stay with vector, there is only a linear time solution. In
that case, you could as well go with std::rotate() from <algorithm>:

std::rotate( cont.begin(), cont.begin()+1, cont.end() );


Best

Kai-Uwe Bux

Thanks, I will use that in my program. I am writing an update and
expansion of a board game I wrote. I am still working on the overall
design. I just worte the part that allows choosing of the unit you
want to move with the mouse. Eventually, I want to be able to use a
mouse button to cycle through the units in the same space. I am
working on the game slowly tocreate the best design I can. To see the
earlier version of the game:
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=10766&lngWId=3
 
C

Cy Edmunds

JoeC said:
I have a vector of elements, how can I take the first element of a
vector and put it in the end and have all the other elements cycle
down? Ex:
1, 2, 3, 4...
cycle
4, 1, 2 ,3

Perhaps you could leave the data alone and change the indexing:

myvec[(j + k) % n]

for instance would effectively rotate the vector of length n by k items.
 
J

JoeC

Cy said:
JoeC said:
I have a vector of elements, how can I take the first element of a
vector and put it in the end and have all the other elements cycle
down? Ex:
1, 2, 3, 4...
cycle
4, 1, 2 ,3

Perhaps you could leave the data alone and change the indexing:

myvec[(j + k) % n]

for instance would effectively rotate the vector of length n by k items.

I am still working out how I am going to design the program. What I
hope to do is to cycle through the units stacked together in the same
space. I am slowly making progress and improvments in the game. I
still have a ways to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top