473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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.

Jul 19 '05 #1
6 37293
On Tue, 24 Jun 2003 03:32:41 +0100,
Vasileios Zografos <vz*******@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

Jul 19 '05 #2
> Surely you have a book with this stuff in it?

Nope. But can you suggest one?

Thank you for the help.
V.Z.

Jul 19 '05 #3

"Vasileios Zografos" <vz*******@bcs.org.uk> wrote in message
news:bd**********@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.
Jul 19 '05 #4
sh*****@flexal.cs.usyd.edu.au (Sam Holden) wrote in message news:<slrnbffeee.cah.sh*****@flexal.cs.usyd.edu.au >...
On Tue, 24 Jun 2003 03:32:41 +0100,
Vasileios Zografos <vz*******@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
Jul 19 '05 #5
> >
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
Jul 19 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message news:bd************@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.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
14
by: Raymond Hettinger | last post by:
Based on the feedback here on comp.lang.python, the pep has been updated: www.python.org/peps/pep-0322.html The key changes are: * reversed() is being preferred to ireverse() as the best...
15
by: Alex Vinokur | last post by:
I am looking for any custom allocator sample code for std::vector. Thanks. -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
13
by: Aamir Mahmood | last post by:
What could be the fastest method to reverse a string. Speed really matters for my application. Average size of string that comes to my function is around 200K - 300K. Currently i am using the...
4
by: Jonathan Wood | last post by:
Is it just me? It seems like one moving from MFC to C# loses some string functionality such as the two mentioned in the subject. Did I miss something? -- Jonathan Wood SoftCircuits...
3
by: analoveu | last post by:
I have a problem with using the reverse method any one give me an exmample or correct my error inside this code String p=""; for(int x=0 ; x<=3 ; x++) p+=x; System.out.print(p); String...
3
by: analoveu | last post by:
I want to know if there is a built-in method in the java class library and ready for use .If there how can i use it to reverse a string and print it I saw i the JDK6.0 Docs this method java.lang...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.