问题
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!
回答1:
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
.
回答2:
dst.insert(dst.end(), src.begin(), src.end() );
来源:https://stackoverflow.com/questions/10163548/add-a-vector-to-a-vector