Connecting Tech Pros Worldwide Help | Site Map

combining two vectors

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 14th, 2006, 10:25 PM
Alan
Guest
 
Posts: n/a
Default combining two vectors

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


  #2  
Old November 14th, 2006, 10:25 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: combining two vectors

Alan wrote:
Quote:
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.
  #3  
Old November 14th, 2006, 10:35 PM
Alan
Guest
 
Posts: n/a
Default Re: combining two vectors

Ian, Thanks! Alan

  #4  
Old November 15th, 2006, 10:05 AM
Stefan Naewe
Guest
 
Posts: n/a
Default Re: combining two vectors

Ian Collins schrieb:
Quote:
Alan wrote:
Quote:
> 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
  #5  
Old November 15th, 2006, 10:35 AM
Pete Becker
Guest
 
Posts: n/a
Default Re: combining two vectors

Stefan Naewe wrote:
Quote:
Ian Collins schrieb:
Quote:
>Alan wrote:
Quote:
>> 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)
  #6  
Old November 15th, 2006, 11:15 AM
Stefan Naewe
Guest
 
Posts: n/a
Default Re: combining two vectors

Pete Becker schrieb:
Quote:
Stefan Naewe wrote:
Quote:
>Ian Collins schrieb:
Quote:
>>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
  #7  
Old November 15th, 2006, 02:25 PM
red floyd
Guest
 
Posts: n/a
Default Re: combining two vectors

Stefan Naewe wrote:
Quote:
Ian Collins schrieb:
Quote:
>Alan wrote:
Quote:
>> 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".
  #8  
Old November 15th, 2006, 03:45 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: combining two vectors

red floyd wrote:
Quote:
Stefan Naewe wrote:
Quote:
>Ian Collins schrieb:
Quote:
>>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?
Quote:
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)
  #9  
Old November 15th, 2006, 06:45 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: combining two vectors

Pete Becker wrote:
Quote:
red floyd wrote:
>
Quote:
>>
>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?
>
Quote:
>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.
  #10  
Old November 15th, 2006, 06:45 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: combining two vectors

* Ian Collins:
Quote:
Pete Becker wrote:
Quote:
>red floyd wrote:
>>
Quote:
>>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?
  #11  
Old November 15th, 2006, 09:45 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: combining two vectors

Alf P. Steinbach wrote:
Quote:
* Ian Collins:
>
Quote:
>Pete Becker wrote:
>>
Quote:
>>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.
  #12  
Old November 15th, 2006, 09:45 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: combining two vectors

* Ian Collins:
Quote:
Alf P. Steinbach wrote:
Quote:
>* Ian Collins:
>>
Quote:
>>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?
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.