473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2638
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.