将向量添加到向量 [英] Add a vector to a vector

查看:147
本文介绍了将向量添加到向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++:将向量附加到向量

我可以轻松地将向量和另一个向量求和吗?我的意思是,push_back一个向量到另一个向量:

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, 8};

{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));
}

还是有一个C ++ / STL的方法?谢谢!

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

推荐答案

可以。 STL方式使用 insert

You can. The STL way is using insert:

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

这会将 second_vector 插入 first_vector first_vector 结尾处开始。

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

这篇关于将向量添加到向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆