454,605 Members | 1,391 Online
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

# combining two vectors

 P: n/a Is there an easy and efficient way to combine two 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 Nov 14 '06 #1
11 Replies

 P: n/a Alan wrote: Is there an easy and efficient way to combine two 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. Nov 14 '06 #2

 P: n/a Ian, Thanks! Alan Nov 14 '06 #3

 P: n/a Ian Collins schrieb: Alan wrote: > Is there an easy and efficient way to combine two s, ratherthan taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of thissort 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 Nov 15 '06 #4

 P: n/a Stefan Naewe wrote: Ian Collins schrieb: >Alan wrote: >> Is there an easy and efficient way to combine two s, ratherthan taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of thissort 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) Nov 15 '06 #5

 P: n/a Pete Becker schrieb: Stefan Naewe wrote: >Ian Collins schrieb: >>Alan wrote: Is there an easy and efficient way to combine two s, ratherthan taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of thissort 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 Nov 15 '06 #6

 P: n/a Stefan Naewe wrote: Ian Collins schrieb: >Alan wrote: >> Is there an easy and efficient way to combine two s, ratherthan taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of thissort 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". Nov 15 '06 #7

 P: n/a red floyd wrote: Stefan Naewe wrote: >Ian Collins schrieb: >>Alan wrote: Is there an easy and efficient way to combine two s, ratherthan taking each element from one and adding it to the other? I haven`t been able to find any guidance on or examples of thissort 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) Nov 15 '06 #8

 P: n/a 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 thebottleneck? An "efficient" program that doesn't work right is not asgood 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 theroot 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. Nov 15 '06 #9

 P: n/a * Ian Collins: Pete Becker wrote: >red floyd wrote: >>Hoare's Law (also attributed to Knuth): "Premature optimization is theroot of all evil". Okay, let's all use bubble sort until we can prove that quicksort willbe 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? Nov 15 '06 #10

 P: n/a Alf P. Steinbach wrote: * Ian Collins: >Pete Becker wrote: >>red floyd wrote:Hoare's Law (also attributed to Knuth): "Premature optimization is theroot of all evil". Okay, let's all use bubble sort until we can prove that quicksort willbe 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 "Prematuremicro-optimisation is the root of all evil" to differentiate betweencode tweaks and choice of algorithm. Isn't that an optimization? More like a design decision. -- Ian Collins. Nov 15 '06 #11

 P: n/a * Ian Collins: Alf P. Steinbach wrote: >* Ian Collins: >>Pete Becker wrote:red floyd wrote:Hoare's Law (also attributed to Knuth): "Premature optimization is theroot of all evil".Okay, let's all use bubble sort until we can prove that quicksort willbe 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 "Prematuremicro-optimisation is the root of all evil" to differentiate betweencode tweaks and choice of algorithm. Isn't that an optimization? More like a design decision. Well, yes, but 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? Nov 15 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.