来自 C++ 的练习 P6.07 适合所有人:编写一个在后面vector<int> append(vector<int> a, vector<int> b)
追加的函数b
a
前任。a is 1 4 9 16
然后b is 9 7 4 9 11
它返回1 4 9 16 9 7 4 9 11
我的功能
vector<int> append(vector<int> a, vector<int> b)
{
vector<int> appended;
for (unsigned int i = 0; i < a.size(); i++)
{
appended.push_back(a[i]);
}
for (unsigned int i = 0; i < b.size(); i++)
{
appended.push_back(b[i]);
}
return appended;
}
我的尝试之一:
int main()
{
cout << "Enter some numbers: ";
int input, input2;
vector<int> a, b;
while (cin >> input)
{
if (cin.fail())
{
cout << "Enter some numbers: ";
while (cin >> input2)
{
if (cin.fail()) {break;}
else {b.push_back(input2);}
}
}
else {a.push_back(input);}
}
return 0;
}
我将如何使用cin
来获取向量a
以及b
何时运行该main()
函数?