red said:
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)