【发布时间】:2021-01-23 18:49:51
【问题描述】:
很长一段时间以来,我一直在研究我认为只是一个简单的功能。我们应该使用push_back(),但我无法让它工作。
这是我现在拥有的:
void append(const vector<int>& v1, vector<int> v2)
{
for (int x : v1)
v2.push_back(x);
}
void println(const vector<int>& v)
{
for (int x : v)
cout << x << ' ';
cout << endl;
}
int main()
{
vector <int> v1(3, 15);
vector <int> v2(3, 25);
append(v1, v2);
println(v1);
println(v2);
}
【问题讨论】:
-
Vectors 有一个
insert函数已经可以做到这一点。 -
您正在传递 v2 以按值附加。通过引用传递它。
标签: c++