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,075
  • 16
  • 56
  • 98

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 Dressler
  • 7,904
  • 2
  • 32
  • 57
1
dst.insert(dst.end(), src.begin(), src.end() );
Martin York
  • 246,832
  • 83
  • 321
  • 542
Daniel
  • 29,121
  • 15
  • 79
  • 134