如何在C ++中的向量中附加向量?
将向量附加到向量中可以简单地通过向量insert()方法来完成。
算法
Begin
Declare a function show().
Pass a constructor of a vector as a parameter within show() function.
for (auto const& i: input)
Print the value of variable i.
Declare vect1 of vector type.
Initialize values in the vect1.
Declare vect2 of vector type.
Initialize values in the vect2.
Call vect2.insert(vect2.begin(), vect1.begin(), vect1.end()) to
append the vect1 into vect2.
Print “结果向量为:”
Call show() function to display the value of vect2.
End.范例程式码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void show(vector<int> const &input) {
for (auto const& i: input) {
std::cout << i << " ";
}
}
int main() {
vector<int> v1 = { 1, 2, 3 };
vector<int> v2 = { 4, 5 };
v2.insert(v2.begin(), v1.begin(), v1.end());
cout<<"结果向量为:"<<endl;
show(v2);
return 0;
}输出结果
结果向量为: 1 2 3 4 5
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短