stl vector reverse method

Discussion in 'C++' started by Vasileios Zografos, Jun 24, 2003.

  1. Hi there I am using the STL vector and I was wondering, is there an
    existing method for reversing the contents of one vector?

    e.g. vector<int> v1 which has entries 1,2,3,4,5
    and I want to end up with v1=5,4,3,2,1 or perhaps leave v1 intact and
    generate a new vector v2=5,4,3,2,1

    so, does the STL have something allready? or do I have to do it manualy?

    Thanks
    V.Z.
     
    Vasileios Zografos, Jun 24, 2003
    #1
    1. Advertising

  2. Vasileios Zografos

    Sam Holden Guest

    On Tue, 24 Jun 2003 03:32:41 +0100,
    Vasileios Zografos <> wrote:
    > Hi there I am using the STL vector and I was wondering, is there an
    > existing method for reversing the contents of one vector?
    >
    > e.g. vector<int> v1 which has entries 1,2,3,4,5
    > and I want to end up with v1=5,4,3,2,1 or perhaps leave v1 intact and
    > generate a new vector v2=5,4,3,2,1
    >
    > so, does the STL have something allready? or do I have to do it manualy?


    std::reverse(v1.begin(), v1.end());

    or:

    std::vector<whatever> v2(v1.size());
    std::reverse_copy(v1.begin(), v1.end(), v2.begin());

    Surely you have a book with this stuff in it?

    Or even: http://www.sgi.com/tech/stl/

    --
    Sam Holden
     
    Sam Holden, Jun 24, 2003
    #2
    1. Advertising

  3. > Surely you have a book with this stuff in it?

    Nope. But can you suggest one?

    Thank you for the help.
    V.Z.
     
    Vasileios Zografos, Jun 24, 2003
    #3
  4. "Vasileios Zografos" <> wrote in message
    news:bd8gip$3nm$...
    > > Surely you have a book with this stuff in it?

    >
    > Nope. But can you suggest one?
    >
    > Thank you for the help.
    > V.Z.
    >


    The Standard C++ library by Josuttis.
     
    John Harrison, Jun 24, 2003
    #4
  5. (Sam Holden) wrote in message news:<>...
    > On Tue, 24 Jun 2003 03:32:41 +0100,
    > Vasileios Zografos <> wrote:
    > > Hi there I am using the STL vector and I was wondering, is there an
    > > existing method for reversing the contents of one vector?
    > >
    > > e.g. vector<int> v1 which has entries 1,2,3,4,5
    > > and I want to end up with v1=5,4,3,2,1 or perhaps leave v1 intact and
    > > generate a new vector v2=5,4,3,2,1
    > >
    > > so, does the STL have something allready? or do I have to do it manualy?

    >
    > std::reverse(v1.begin(), v1.end());
    >
    > or:
    >
    > std::vector<whatever> v2(v1.size());
    > std::reverse_copy(v1.begin(), v1.end(), v2.begin());


    The latter can also be written as

    std::vector< > v2( v1.rbegin(), v1.rend() );

    which saves v1.size() default-initializations of whatevers (which might
    be impossible, default ctors aren't mandatory)

    Regards,
    --
    Michiel Salters
     
    Michiel Salters, Jun 24, 2003
    #5
  6. > >
    > > std::vector<whatever> v2(v1.size());
    > > std::reverse_copy(v1.begin(), v1.end(), v2.begin());

    >
    > The latter can also be written as
    >
    > std::vector< > v2( v1.rbegin(), v1.rend() );
    >
    > which saves v1.size() default-initializations of whatevers (which might
    > be impossible, default ctors aren't mandatory)
    >
    > Regards,
    > --
    > Michiel Salters


    Default ctors are mandatory to be compliant with the STL.

    john
     
    John Harrison, Jun 24, 2003
    #6
  7. Vasileios Zografos

    Ron Natalie Guest

    "John Harrison" <> wrote in message news:bd9u6r$qaj1r$...

    > > which saves v1.size() default-initializations of whatevers (which might
    > > be impossible, default ctors aren't mandatory)
    > >
    > > Regards,
    > > --
    > > Michiel Salters

    >
    > Default ctors are mandatory to be compliant with the STL.


    Default constructors are NOT mandatory. The requirement is that the contents
    be copy constructable and assignable. The few functions that need to be able
    to create "defaulted" objects take a parameter (defaulted to a default constructed
    object) that is copied to these objects. If you provide a non-default constructed
    object for these functions, your object does not need default constructor.
     
    Ron Natalie, Jun 24, 2003
    #7
    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. dogbite
    Replies:
    4
    Views:
    732
    osmium
    Oct 10, 2003
  2. Dennis
    Replies:
    1
    Views:
    2,626
    Dennis
    Jun 7, 2004
  3. Replies:
    8
    Views:
    2,002
    Csaba
    Feb 18, 2006
  4. Rushikesh Joshi
    Replies:
    0
    Views:
    399
    Rushikesh Joshi
    Jul 10, 2004
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    437
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page