stl vector reverse method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vasileios Zografos

    stl vector reverse method

    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.

  • Sam Holden

    #2
    Re: stl vector reverse method

    On Tue, 24 Jun 2003 03:32:41 +0100,
    Vasileios Zografos <vzografos@bcs. org.uk> wrote:[color=blue]
    > 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?[/color]

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

    or:

    std::vector<wha tever> v2(v1.size());
    std::reverse_co py(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

    Comment

    • Vasileios Zografos

      #3
      Re: stl vector reverse method

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

      Nope. But can you suggest one?

      Thank you for the help.
      V.Z.

      Comment

      • John Harrison

        #4
        Re: stl vector reverse method


        "Vasileios Zografos" <vzografos@bcs. org.uk> wrote in message
        news:bd8gip$3nm $1@news8.svr.po l.co.uk...[color=blue][color=green]
        > > Surely you have a book with this stuff in it?[/color]
        >
        > Nope. But can you suggest one?
        >
        > Thank you for the help.
        > V.Z.
        >[/color]

        The Standard C++ library by Josuttis.


        Comment

        • Michiel Salters

          #5
          Re: stl vector reverse method

          sholden@flexal. cs.usyd.edu.au (Sam Holden) wrote in message news:<slrnbffee e.cah.sholden@f lexal.cs.usyd.e du.au>...[color=blue]
          > On Tue, 24 Jun 2003 03:32:41 +0100,
          > Vasileios Zografos <vzografos@bcs. org.uk> wrote:[color=green]
          > > 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?[/color]
          >
          > std::reverse(v1 .begin(), v1.end());
          >
          > or:
          >
          > std::vector<wha tever> v2(v1.size());
          > std::reverse_co py(v1.begin(), v1.end(), v2.begin());[/color]

          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

          Comment

          • John Harrison

            #6
            Re: stl vector reverse method

            > >[color=blue][color=green]
            > > std::vector<wha tever> v2(v1.size());
            > > std::reverse_co py(v1.begin(), v1.end(), v2.begin());[/color]
            >
            > 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[/color]

            Default ctors are mandatory to be compliant with the STL.

            john


            Comment

            • Ron Natalie

              #7
              Re: stl vector reverse method


              "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:bd9u6r$qaj 1r$1@ID-196037.news.dfn cis.de...
              [color=blue][color=green]
              > > which saves v1.size() default-initializations of whatevers (which might
              > > be impossible, default ctors aren't mandatory)
              > >
              > > Regards,
              > > --
              > > Michiel Salters[/color]
              >
              > Default ctors are mandatory to be compliant with the STL.[/color]

              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.


              Comment

              Working...