Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   stl vector reverse method (http://www.velocityreviews.com/forums/t267641-stl-vector-reverse-method.html)

Vasileios Zografos 06-24-2003 02:32 AM

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 06-24-2003 02:39 AM

Re: stl vector reverse method
 
On Tue, 24 Jun 2003 03:32:41 +0100,
Vasileios Zografos <vzografos@bcs.org.uk> 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


Vasileios Zografos 06-24-2003 03:29 AM

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.


John Harrison 06-24-2003 05:06 AM

Re: stl vector reverse method
 

"Vasileios Zografos" <vzografos@bcs.org.uk> wrote in message
news:bd8gip$3nm$1@news8.svr.pol.co.uk...
> > 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.



Michiel Salters 06-24-2003 09:41 AM

Re: stl vector reverse method
 
sholden@flexal.cs.usyd.edu.au (Sam Holden) wrote in message news:<slrnbffeee.cah.sholden@flexal.cs.usyd.edu.au >...
> On Tue, 24 Jun 2003 03:32:41 +0100,
> Vasileios Zografos <vzografos@bcs.org.uk> 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

John Harrison 06-24-2003 04:28 PM

Re: stl vector reverse method
 
> >
> > 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



Ron Natalie 06-24-2003 04:49 PM

Re: stl vector reverse method
 

"John Harrison" <john_andronicus@hotmail.com> wrote in message news:bd9u6r$qaj1r$1@ID-196037.news.dfncis.de...

> > 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.




All times are GMT. The time now is 05:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.