Is there an easy and efficient way to combine two <vector>s, rather
than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this
sort of operation.
Thanks, Alan 11 10572
Alan wrote:
Is there an easy and efficient way to combine two <vector>s, rather
than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this
sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
--
Ian Collins.
Ian Collins schrieb:
Alan wrote:
> Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
It's easy - yes.
But is it really efficient ?
Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
Stefan Naewe wrote:
Ian Collins schrieb:
>Alan wrote:
>> Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
It's easy - yes.
But is it really efficient ?
Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
No. The iterators that represent the range being copied are random
access iterators, so insert can figure out how many elements will be
added and adjust the size accordingly. Repeatedly calling push_back
could end up reallocating the storage space more than once.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
Pete Becker schrieb:
Stefan Naewe wrote:
>Ian Collins schrieb:
>>Alan wrote: Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() ); It's easy - yes. But is it really efficient ? Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
No. The iterators that represent the range being copied are random
access iterators, so insert can figure out how many elements will be
added and adjust the size accordingly. Repeatedly calling push_back
could end up reallocating the storage space more than once.
OK.
But what if I do a vector1.reserve(x) (with a valid x...) before ?
'vector2.end()-vector2.begin()' elements need to be copied after vector1.end(), right?
(Using std::list and std::list::splice() would be more efficient in this case, wouldn't it?)
/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
Stefan Naewe wrote:
Ian Collins schrieb:
>Alan wrote:
>> Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
It's easy - yes.
But is it really efficient ?
Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
Why does everyone ask "is this more efficient" in a vacuum?
Have you benchmarked to determine that your vector operations are the
bottleneck? An "efficient" program that doesn't work right is not as
good as an "inefficient" program that does.
Hoare's Law (also attributed to Knuth): "Premature optimization is the
root of all evil".
red floyd wrote:
Stefan Naewe wrote:
>Ian Collins schrieb:
>>Alan wrote: Is there an easy and efficient way to combine two <vector>s, rather than taking each element from one and adding it to the other?
I haven`t been able to find any guidance on or examples of this sort of operation.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() ); It's easy - yes. But is it really efficient ? Isn't it as unefficient as repeatedly calling push_bask() on vector1 ?
Why does everyone ask "is this more efficient" in a vacuum?
Have you benchmarked to determine that your vector operations are the
bottleneck? An "efficient" program that doesn't work right is not as
good as an "inefficient" program that does.
And how is changing from push_back to insert going to make this code not
work right?
Hoare's Law (also attributed to Knuth): "Premature optimization is the
root of all evil".
Okay, let's all use bubble sort until we can prove that quicksort will
be a better choice.
Sometimes optimization without measurement is perfectly appropriate.
When there's a choice of two ways to do something and one is slower,
choose the other.
for (iter = vector2.begin(); iter != vector2.end(); ++iter)
vector1.push_back(*iter);
versus
vector1.insert(vector1.end(), vector2.begin(), vector2.end());
No contest. Use the latter.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
Pete Becker wrote:
red floyd wrote:
>> Why does everyone ask "is this more efficient" in a vacuum?
Have you benchmarked to determine that your vector operations are the bottleneck? An "efficient" program that doesn't work right is not as good as an "inefficient" program that does.
And how is changing from push_back to insert going to make this code not
work right?
>Hoare's Law (also attributed to Knuth): "Premature optimization is the root of all evil".
Okay, let's all use bubble sort until we can prove that quicksort will
be a better choice.
Sometimes optimization without measurement is perfectly appropriate.
When there's a choice of two ways to do something and one is slower,
choose the other.
Agreed, maybe the quote should be changed to "Premature
micro-optimisation is the root of all evil" to differentiate between
code tweaks and choice of algorithm.
--
Ian Collins.
* Ian Collins:
Pete Becker wrote:
>red floyd wrote:
>>Hoare's Law (also attributed to Knuth): "Premature optimization is the root of all evil".
Okay, let's all use bubble sort until we can prove that quicksort will be a better choice.
Sometimes optimization without measurement is perfectly appropriate. When there's a choice of two ways to do something and one is slower, choose the other.
Agreed, maybe the quote should be changed to "Premature
micro-optimisation is the root of all evil" to differentiate between
code tweaks and choice of algorithm.
Isn't that an optimization?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote:
* Ian Collins:
>Pete Becker wrote:
>>red floyd wrote:
Hoare's Law (also attributed to Knuth): "Premature optimization is the root of all evil".
Okay, let's all use bubble sort until we can prove that quicksort will be a better choice.
Sometimes optimization without measurement is perfectly appropriate. When there's a choice of two ways to do something and one is slower, choose the other.
Agreed, maybe the quote should be changed to "Premature micro-optimisation is the root of all evil" to differentiate between code tweaks and choice of algorithm.
Isn't that an optimization?
More like a design decision.
--
Ian Collins.
* Ian Collins:
Alf P. Steinbach wrote:
>* Ian Collins:
>>Pete Becker wrote:
red floyd wrote:
Hoare's Law (also attributed to Knuth): "Premature optimization is the root of all evil".
Okay, let's all use bubble sort until we can prove that quicksort will be a better choice.
Sometimes optimization without measurement is perfectly appropriate. When there's a choice of two ways to do something and one is slower, choose the other.
Agreed, maybe the quote should be changed to "Premature micro-optimisation is the root of all evil" to differentiate between code tweaks and choice of algorithm.
Isn't that an optimization?
More like a design decision.
Well, yes, but isn't that an optimization? <g>
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pratyush |
last post by:
Hi,
Suppose there is a vector of objects of class A, i.e., std::vector<A>
vec_A(N); The class A satisifies all the STL vector requirements.
Now I wish to add some attributes for each of the...
|
by: Chris Mullins |
last post by:
I've spent a bit of time over the last year trying to implement RFC 3454
(Preparation of Internationalized Strings, aka 'StringPrep').
This RFC is also a dependency for RFC 3491...
|
by: vasileios zografos |
last post by:
Hi,
I am trying to combine two stl vectors into one
so something like
std::vector<int> vector1;
std::vector<int> vector2;
std::vector<int> FullVector;
|
by: Computer Whizz |
last post by:
I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.
Is there any benefit of Arrays over Vectors? Since all Vectors...
|
by: Amit |
last post by:
Hello.
I am having some problem organizing a set of vectors. The vectors itself,
could contain a pointer( say integer pointer) or could contain another
object MyClass.
1>So, first of all, is...
|
by: Dr. J.K. Becker |
last post by:
Hi all,
I have vectors that holds pointers to other vectors, like so:
vector<whatever> x;
vector<whatever*> z;
z=&x;
Now I add something to x
|
by: madhu |
last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
vector <intv1;
v1.push_back( 10 ); //adds 10 to the tail
v1.push_back( 20 ); //adds 20 to the tail
cout << "The size of v1 is " <<...
|
by: wuzertheloser |
last post by:
Use the program skeleton below (starting with #include <stdio.h>)
as the starting point for quiz4. Add the necessary code to the
functions prob1() and prob2(), and add the other 2 functions, as...
|
by: John Salerno |
last post by:
Before I try this and destroy my computer :) I just wanted to see if
this would even work at all. Is it possible to read a binary file such
as an mp3 or an avi, put its contents into a new file,...
|
by: Rob |
last post by:
How would I do this?
I want to be able to handle vectors of many different types of data
and vectors that can contain any number of other vectors of data.
Currently, I have a templated...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |