将向量附加到向量 [英] Appending a vector to a vector
本文介绍了将向量附加到向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有2个标准向量:
Assuming I have 2 standard vectors:
vector<int> a;
vector<int> b;
让我们还说两者都有大约30个元素。
Let's also say the both have around 30 elements.
- 如何将向量b添加到向量a的末尾?
方法将通过b进行迭代,并通过向量< int> :: push_back()
添加每个元素,但我不想这样做!
The dirty way would be iterating through b and adding each element via vector<int>::push_back()
, though I wouldn't like to do that!
推荐答案
a.insert(a.end(), b.begin(), b.end());
或
a.insert(std::end(a), std::begin(b), std::end(b));
第二个变体是一个更通用的解决方案,因为 b
也可以是一个数组。但是,它需要C ++ 11
The second variant is a more generically applicable solution, as b
could also be an array. However, it requires C++11
这篇关于将向量附加到向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文