473,405 Members | 2,279 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,405 software developers and data experts.

array shifting

Ken
Hello,

I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or right, So a
left shit will make the first number come last etc..
I am not sure about the way.
I found that if I duplicated the array, put it in a big array( put them side
by side )and just trim of what I dont need.
It seems ok but not efficient,
any ideas?

thanks
ken
Jul 22 '05 #1
9 15516
* Ken:

I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or right, So a
left shit will make the first number come last etc..
I am not sure about the way.
I found that if I duplicated the array, put it in a big array( put them side
by side )and just trim of what I dont need.
It seems ok but not efficient,
any ideas?


Recall that C++ is C + Classes.

Create a class that provides an [] operator.

Inside each instance use an offset for indexing, and remember to
wrap around (e.g. using the % operator).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
If You need "God-speed" for shifting, try using circural list and remember
pointer to first element. To shift left/right just move that pointer to
one node right or left. With this shiting time is K where K is number that
represents how mutch You shift array (cost of using this is slower
indexing).

Best,
Zaharije Pasalic

Jul 22 '05 #3
> I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or right, So a
left shit will make the first number come last etc..
I am not sure about the way.
I found that if I duplicated the array, put it in a big array( put them side
by side )and just trim of what I dont need.
It seems ok but not efficient,
any ideas?


Depends on:
* what you have to rotate
* the rotate/access ratio
* whether you need random access to the elements or not
* whether you must be able to grow the collection or not
* whether you need to rotate by 1/-1 or by arbitrary amounts
* whether it's plain data (POD) or not
* whether you need that data/objects to reside in a continuous
memory-block with no single break or not

bye,
'monster
Jul 22 '05 #4
Ken

"Swampmonster" <sw**********@der-ball-ist-rund.net> wrote in message
news:11***************@news.liwest.at...
I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or right, So
a left shit will make the first number come last etc..
I am not sure about the way.
I found that if I duplicated the array, put it in a big array( put them
side by side )and just trim of what I dont need.
It seems ok but not efficient,
any ideas?


Depends on:
* what you have to rotate
* the rotate/access ratio
* whether you need random access to the elements or not
* whether you must be able to grow the collection or not
* whether you need to rotate by 1/-1 or by arbitrary amounts
* whether it's plain data (POD) or not
* whether you need that data/objects to reside in a continuous
memory-block with no single break or not

bye,
'monster


Well ther is two types,
one: have an array 1,2,3,4,5 and have it rotate left or right so output
will be 2,3,4,5,1 or 5,1,2,3,4

two: removing or adding on to it, from 1,2,3,4,5 to 1,2,2,3,4,5 or
1,2,4,5,

ken
Jul 22 '05 #5

Ken wrote:
Hello,

I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or right, So a left shit will make the first number come last etc..
I am not sure about the way.


Sounds like you want to rotate the array. Luckily, there's a function
std::rotate(first,middle,last). It will shift the first element to
middle
etcetera, and what doesn't fit at the end is put back at the begin
i.e. shift left over (middle-first).

Regards,
Michiel Salters

Jul 22 '05 #6
Ken

"msalters" <Mi*************@logicacmg.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Ken wrote:
Hello,

I would a hint for the following,
I have an array, and I wabt to make it shift, to either left or

right, So a
left shit will make the first number come last etc..
I am not sure about the way.


Sounds like you want to rotate the array. Luckily, there's a function
std::rotate(first,middle,last). It will shift the first element to
middle
etcetera, and what doesn't fit at the end is put back at the begin
i.e. shift left over (middle-first).

Regards,
Michiel Salters


I have done some research on list class, with commands like push_back ,
puch_front etc..
I could not find any simple program that would show me how it works,
Is it possible to do program that is only 10-15 lines with this implemented
into it?

ken
Jul 22 '05 #7
> I have done some research on list class, with commands like push_back ,
puch_front etc..
I could not find any simple program that would show me how it works,
Is it possible to do program that is only 10-15 lines with this implemented
into it?


#include <list>
#include <iterator>
#include <iostream>

inline void foomanchoo()
{
std::list<int> l;

l.push_back( 2 );
l.push_back( 3 );
l.push_back( 4 );
l.push_front( 1 );
// list should now be (1,2,3,4)

// dump to cout
std::cout << "list contents: \n";
std::copy( l.begin(), l.end(), std::ostream_iterator<int>( std::cout,
"\n" ) );

// rotate "left" by 1
l.push_back(
l.front() // this is a ref to the first element
);
l.pop_front();
// list should now be (2,3,4,1)

// dump to cout again
std::cout << "new list contents: \n";
std::copy( l.begin(), l.end(), std::ostream_iterator<int>( std::cout,
"\n" ) );
}
Jul 22 '05 #8
Ken

"Swampmonster" <sw**********@der-ball-ist-rund.net> wrote in message
news:11***************@news.liwest.at...
I have done some research on list class, with commands like push_back ,
puch_front etc..
I could not find any simple program that would show me how it works,
Is it possible to do program that is only 10-15 lines with this
implemented into it?


#include <list>
#include <iterator>
#include <iostream>

inline void foomanchoo()
{
std::list<int> l;

l.push_back( 2 );
l.push_back( 3 );
l.push_back( 4 );
l.push_front( 1 );
// list should now be (1,2,3,4)

// dump to cout
std::cout << "list contents: \n";
std::copy( l.begin(), l.end(), std::ostream_iterator<int>( std::cout,
"\n" ) );

// rotate "left" by 1
l.push_back(
l.front() // this is a ref to the first element
);
l.pop_front();
// list should now be (2,3,4,1)

// dump to cout again
std::cout << "new list contents: \n";
std::copy( l.begin(), l.end(), std::ostream_iterator<int>( std::cout,
"\n" ) );
}


Yes short and sweet , thanks
I assume that this is working like a class.
thanks

ken
Jul 22 '05 #9
Ken wrote:
"msalters" <Mi*************@logicacmg.com> wrote in message
Ken wrote:
Hello,

I would a hint for the following,
I have an array, and I wabt to make it shift, to either left orright, So a
left shit will make the first number come last etc..
I am not sure about the way.


Sounds like you want to rotate the array. Luckily, there's a function
std::rotate(first,middle,last). It will shift the first element to
middle
etcetera, and what doesn't fit at the end is put back at the begin
i.e. shift left over (middle-first).


I have done some research on list class, with commands like push_back ,
puch_front etc..


Did you check std::rotate?
I could not find any simple program that would show me how it works,
Is it possible to do program that is only 10-15 lines with this implemented
into it?


# include <vector>
# include <algorithm>
# include <iostream>

int main()
{
std::vector<int> v;
for (int i=0; i<10; ++i)
v.push_back(i);

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout));
std::cout << std::endl;

std::rotate(v.begin(), v.begin() + 5, v.end());

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout));
}

Output:
0123456789
5678901234

Jonathan
Jul 22 '05 #10

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

Similar topics

9
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
14
by: vib | last post by:
Hi there, union UC32 { unsigned int L; unsigned short S; unsigned char C; } UC32; with the above structure, I am able to shift bits of C, into C, and C into C so on and so forth till C as...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
0
by: Mark Gibson | last post by:
Hi, I've been playing about with array's, and found the concat operator '||' quite useful, apart from the fact that prepending an element places it in a lower subscript. Is there a way of...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
4
by: sam | last post by:
hI, I am little confused here See i have int wordlen=10; when int s is array s++; whats the meaning of this
7
by: Bint | last post by:
Hi, What is a simple way to shift the elements in an array, circularly? Is there a way to do it so that you don't need a separate storage array? IE I want to shift array A{1,2,3,4,5,6,7,8} by...
8
by: omidsoltan | last post by:
Hello, I am new to c++ and have a very simple question. Why when reading a char array the whole array is displayed for example char st = " hello " ; cout << st << endl ; will then display...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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,...

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.