473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

cycling through a vector?

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

Oct 27 '06 #1
5 2642
JoeC wrote:
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.
Oct 27 '06 #2
JoeC wrote:
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
Oct 27 '06 #3

Kai-Uwe Bux wrote:
JoeC wrote:
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
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/s...10766&lngWId=3

Oct 27 '06 #4

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>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.
Oct 28 '06 #5

Cy Edmunds wrote:
"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
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.

Oct 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
3
by: robbiehenry | last post by:
1. robbiehe...@gmail.com Sep 19, 1:48 pm show options From: robbiehe...@gmail.com - Find messages by this author Date: Mon, 19 Sep 2005 10:48:50 -0700 Local: Mon, Sep 19 2005 1:48 pm...
5
by: -:= Cactus | last post by:
Hi! I've made a form for dataentry in a simple table. However when there are records in that table and I open the form it only displays the new (blank) record. The total number of records is 1,...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
by: JoeC | last post by:
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: My vector holds pointers vector<*ele>vec; 1, 2, 3,...
4
by: crescent_au | last post by:
Hi all, I'm doing some research online on creating php-based multi-level marketing (MLM) system. It seems like a complicated system to build as I need to create one from scratch. I'd like to...
8
by: Rajneesh Chellapilla | last post by:
Hi I have a general question about cycling background colors. when using background color: rgb(0,0,0) is it ok to write if {i<250){
3
by: jock1up | last post by:
I am working with javascript...where I have cycling banners at the top, 3 of them and I need to make two of them clickable to a website that I assign to the two. bannerad2 for www.bigmtn.com and...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.