473,406 Members | 2,369 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,406 software developers and data experts.

Transferring vector content

I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?
Jul 23 '05 #1
10 2215
tuvok wrote:
I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?

Depends on a few things:

1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what's wrong with
v2 = v1;

Presumably the implementation won't have any easier time figuring
out what you're doing and optimizing than the simple copy semantics.
Jul 23 '05 #2
"Ron Natalie" wrote
tuvok wrote:
I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?

Depends on a few things:

1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what's wrong with
v2 = v1;

Presumably the implementation won't have any easier time figuring
out what you're doing and optimizing than the simple copy semantics.


I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?
Jul 23 '05 #3
"tuvok" <52***************@t-online.de> wrote in message
news:d8*************@news.t-online.com...
"Ron Natalie" wrote
tuvok wrote:
I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?

Depends on a few things:

1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what's wrong with
v2 = v1;

Presumably the implementation won't have any easier time figuring
out what you're doing and optimizing than the simple copy semantics.


I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?

Do you have to use a std::vector? If you used std::list
instead, you'd have use of the "splice" method.
"splice" would just re-interpret the elements of V1
as being elements tacked-on to the end of V2, leaving
V1 empty. Blazingly fast, because no actual elements
are moved! Just a pointer or two is changed.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #4
tuvok wrote:
"Ron Natalie" wrote
tuvok wrote:
> I have such vectors:
> std::vector<T> v1, v2;
> and need to transfer all items of v1 to v2.
> What is the fastest method?
>
>

Depends on a few things:

1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what's wrong with
v2 = v1;

Presumably the implementation won't have any easier time figuring
out what you're doing and optimizing than the simple copy semantics.


I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?


Read the posting you answered to again. It already contains the solution.
If v2 was empty before, v1 will be empty afterwards. If not, just add
v1.clear();

Jul 23 '05 #5
On 2005-06-16 06:01:20 -0400, "tuvok" <52***************@t-online.de> said:
"Ron Natalie" wrote
tuvok wrote:
I have such vectors:
std::vector<T> v1, v2;
and need to transfer all items of v1 to v2.
What is the fastest method?

Depends on a few things:

1. Do you need to leave v1 intact?
If not:
std::swap(v1, v2);
2. Otherwise, what's wrong with
v2 = v1;

Presumably the implementation won't have any easier time figuring
out what you're doing and optimizing than the simple copy semantics.


I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?


Well, then Ron already gave you the answer you want:

//This will swap the contents of v1 and v2
std::swap(v1,v2);

//This will make v1 empty
v1.clear();

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #6
"tuvok" <52***************@t-online.de> wrote in message
news:d8*************@news.t-online.com...
Best would be if it
shrinks to 0 elems without calling any item dtor.


You may cause a leak in memory if you do that. Don't do it.
Jul 23 '05 #7
tuvok wrote:

I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?

Then swap is your answer. Many implementations specialize swap
to move the vector internals between the two objects. You
move the internal array from v1 to v2 and v2's empty array
into v1.
Jul 23 '05 #8
On 2005-06-16 13:21:41 -0400, Ron Natalie <ro*@spamcop.net> said:
tuvok wrote:

I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?

Then swap is your answer. Many implementations specialize swap
to move the vector internals between the two objects. You
move the internal array from v1 to v2 and v2's empty array
into v1.


There's virtually no other way to implement swap for vectors,
considering that it is required to be constant time.

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #9
Clark S. Cox III wrote:
On 2005-06-16 13:21:41 -0400, Ron Natalie <ro*@spamcop.net> said:
tuvok wrote:

I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?

Then swap is your answer. Many implementations specialize swap
to move the vector internals between the two objects. You
move the internal array from v1 to v2 and v2's empty array
into v1.

There's virtually no other way to implement swap for vectors,
considering that it is required to be constant time.


vector::swap is required to run in constant time, because containers
may only provide a swap function if it can run in constant time.
std::swap<T> _is not_ required to run in constant time for arbitrary T.
Since vector::swap is a simple member function and therefor no
specialization I strongly assume that std::swap was ment.

Btw.: vector::swap can only run in constant time if the allocator
is equal (i.e. operator == on the 2 stored allocators returns true).
Though if one uses the standard allocater this should not be a
problem.
Jul 23 '05 #10


Swampmonster wrote:
Clark S. Cox III wrote:
On 2005-06-16 13:21:41 -0400, Ron Natalie <ro*@spamcop.net> said:
tuvok wrote:
I'm currently using the 2nd version, but was looking for a faster way.
v1 does not need to be intact after the transfer. Best would be if it
shrinks to 0 elems without calling any item dtor. Is this possible?
Then swap is your answer. Many implementations specialize swap
to move the vector internals between the two objects. You
move the internal array from v1 to v2 and v2's empty array
into v1.

There's virtually no other way to implement swap for vectors,
considering that it is required to be constant time.


vector::swap is required to run in constant time, because containers
may only provide a swap function if it can run in constant time.
std::swap<T> _is not_ required to run in constant time for arbitrary T.
Since vector::swap is a simple member function and therefor no
specialization I strongly assume that std::swap was ment.

Btw.: vector::swap can only run in constant time if the allocator
is equal (i.e. operator == on the 2 stored allocators returns true).
Though if one uses the standard allocater this should not be a
problem.


It's a given that the allocators of both vectors will be of the same
type; since the allocator employed by the vector is part of the
vector's type and both std:: and vector::swap require identical types.

The point about the different swap() routines is a good one. Since it's
known that calling vector::swap() is the fastest way to transfer the
contents of one vector to another, why call std::swap() and hope that
it calls vector::swap() (it should) and not buy some peace of mind by
calling vector::swap() directly?

After all the last function one would want to call to perform the
transfer would be the general swap implementation. If the general case
std::swap() were somehow called it would be twice as slow as a simple
assignment; and the simple assignment is already too slow. As has been
pointed out vector::swap() executes in constant time regardless of the
size of the vectors, whereas an assignment operation slows as the
vector grows... [the C++ poetry has been added for cultural enrichment.
Please do not send requests for addition poems in this genre. It will
be necessary to wait for my six volume set of C++ poetry due some time
in the fall.. :-) ].
Greg

Jul 23 '05 #11

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

Similar topics

2
by: CJ Oxx | last post by:
Is there an easy way to transfer a page POST content to the next page? Right now I read out the POST content in the PHP script and put it in a html form (with POST as content type) as a hidden...
2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
6
by: Dr. J.K. Becker | last post by:
Hi all, Is there any way to get a pointer to a vector (or the vector itself) if you only have an iterator of it? Something like this: DoSomething(vector<double>::iterator x) { vector<double>...
1
by: Asaf | last post by:
Hi, I am developing a windows forms client application that will send a large XML data to a web server using Web Services. I saw this article http://www.codeproject.com/soap/MTOMWebServices.asp...
28
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open...
3
by: newbie | last post by:
Thanks for helping on this problem suppose I have the following class class MyContent { vector<MyType1 v1; vector<MyType2 v2; } And I have a templated function
10
by: nerdrakesh | last post by:
Hi - Is there a method in STL vector to get the elements as an array instead of as a vector. Something like vector<intvt; vt.push_back(1); vt.push_back(2);
7
by: Hansen | last post by:
Hi group, I'm writting a test at the moment where I want to inspect the content of a vector that I uses as a buffer. The problem is that the function which populates the vector also sends the...
2
by: Kazzie | last post by:
I have a structure struct Rulestrc { char* rulen; vector<char*>v6; char* resp; }; I then create an array of structures - Rulestrc tp. However the content of tp is constantly changing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.