array shifting

Discussion in 'C++' started by Ken, Dec 16, 2004.

  1. Ken

    Ken Guest

    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
    Ken, Dec 16, 2004
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Dec 16, 2004
    #2
    1. Advertising

  3. Ken

    hari4063 Guest

    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
    hari4063, Dec 16, 2004
    #3
  4. Ken

    Swampmonster Guest

    > 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
    Swampmonster, Dec 16, 2004
    #4
  5. Ken

    Ken Guest

    "Swampmonster" <> wrote in message
    news:...
    >> 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
    Ken, Dec 16, 2004
    #5
  6. Ken

    msalters Guest

    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
    msalters, Dec 16, 2004
    #6
  7. Ken

    Ken Guest

    "msalters" <> wrote in message
    news:...
    >
    > 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
    Ken, Dec 17, 2004
    #7
  8. Ken

    Swampmonster Guest

    > 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" ) );
    }
    Swampmonster, Dec 17, 2004
    #8
  9. Ken

    Ken Guest

    "Swampmonster" <> wrote in message
    news:...
    >> 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" ) );
    > }


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

    ken
    Ken, Dec 17, 2004
    #9
  10. Ken wrote:
    > "msalters" <> 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 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).

    >
    > 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
    Jonathan Mcdougall, Dec 20, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    5,320
    Brent Hayhoe
    Nov 21, 2003
  2. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    432
    Stefan Duenser
    Dec 8, 2004
  3. Replies:
    1
    Views:
    454
  4. go6ko01

    Shifting Numbers Inside Array

    go6ko01, Nov 20, 2009, in forum: C Programming
    Replies:
    0
    Views:
    318
    go6ko01
    Nov 20, 2009
  5. Surya

    Shifting string in an array

    Surya, Jan 28, 2013, in forum: C Programming
    Replies:
    8
    Views:
    225
    Shao Miller
    Jan 28, 2013
Loading...

Share This Page