Add a vector to a vector [duplicate]

一个人想着一个人 提交于 2019-12-24 17:48:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!