array shifting

K

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
 
A

Alf P. Steinbach

* 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).
 
H

hari4063

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
 
S

Swampmonster

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
 
K

Ken

Swampmonster said:
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
 
M

msalters

Ken said:
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
 
K

Ken

msalters said:
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
 
S

Swampmonster

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::eek:stream_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::eek:stream_iterator<int>( std::cout,
"\n" ) );
}
 
K

Ken

Swampmonster said:
#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::eek:stream_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::eek:stream_iterator<int>( std::cout,
"\n" ) );
}

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

ken
 
J

Jonathan Mcdougall

Ken said:
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::eek:stream_iterator<int>(std::cout));
std::cout << std::endl;

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

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

Output:
0123456789
5678901234

Jonathan
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top