Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl vector reverse method

Reply
Thread Tools

stl vector reverse method

 
 
Vasileios Zografos
Guest
Posts: n/a
 
      06-24-2003
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.

 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      06-24-2003
On Tue, 24 Jun 2003 03:32:41 +0100,
Vasileios Zografos <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Vasileios Zografos
Guest
Posts: n/a
 
      06-24-2003
> Surely you have a book with this stuff in it?

Nope. But can you suggest one?

Thank you for the help.
V.Z.

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-24-2003

"Vasileios Zografos" <(E-Mail Removed)> wrote in message
news:bd8gip$3nm$(E-Mail Removed)...
> > 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.


 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      06-24-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Sam Holden) wrote in message news:<(E-Mail Removed) >...
> On Tue, 24 Jun 2003 03:32:41 +0100,
> Vasileios Zografos <(E-Mail Removed)> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-24-2003
> >
> > 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      06-24-2003

"John Harrison" <(E-Mail Removed)> wrote in message news:bd9u6r$qaj1r$(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments