1

Possible Duplicate:
C++: Appending a vector to a vector

Can I easily sum a vector to another vector? What I mean is, push_back a vector to another vector:

{1, 2, 3} + {4, 8} = {1, 2, 3, 4, 8};

Do I have to do this manually:

for (int i = 0; i < to_sum_vector.size(); i++) {
    first_vector.push_back(to_sum_vector.at(i));
}

Or is there a C++/STL way of doing it? Thank you!

Community
  • 1
  • 1
David Gomes
  • 5,644
  • 16
  • 60
  • 103
  • Also of: [how to concat two stl vectors?](http://stackoverflow.com/questions/201718/how-to-concat-two-stl-vectors) – Sebastian Apr 15 '12 at 15:56

2 Answers2

4

You can. The STL way is using insert:

first_vector.insert(first_vector.end(), second_vector.begin(), second_vector.end());

This inserts second_vector into first_vector beginning at the end of first_vector.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
1
dst.insert(dst.end(), src.begin(), src.end() );
Martin York
  • 257,169
  • 86
  • 333
  • 562
Daniel
  • 30,896
  • 18
  • 85
  • 139