业余C ++学生
赋值是创建一个函数,它接受两个字符串向量,并返回第三个向量,它以交替的形式组合前两个的内容,最后是剩余的任何剩余。
它编译并运行,但在显示内容之前要求输入,一次一行。请不要花哨,我只想知道我搞砸了哪里。谢谢!
vector<string> shuffle(vector<string> L1, vector<string> L2);
int main() {
string s;
vector<string> L1;
vector<string> L2;
cout << "Enter words for the first list\n";
while (cin >> s && s != "END") {
L1.push_back(s);
}
cout << "Enter words for the second list\n";
while (cin >> s && s != "END") {
L2.push_back(s);
}
shuffle(L1, L2);
for (size_t k = 0; k < L3.size(); k++) {
cout << L3[k] << endl;
}
cout << endl;
return 0;
}
vector<string> shuffle(vector<string> L1, vector<string> L2) {
vector<string> L3;
string s;
for (size_t i = 0; i < L1.size() && i < L2.size(); i++) {
if (i < L1.size()) {
cout << L1[i] << " ";
cin >> s;
L3.push_back(s);
}
if (i < L2.size()) {
cout << L2[i] << " ";
cin >> s;
L3.push_back(s);
}
}
if (L1.size() > L2.size()) {
for (size_t j = 0; j < (L1.size()-L2.size()); j++) {
cout << L1[j+L2.size()] << " ";
cin >> s;
L3.push_back(s);
}
}
if (L2.size() > L1.size()) {
for (size_t j = 0; j < (L2.size()-L1.size()); j++) {
cout << L2[j+L1.size()] << " ";
cin >> s;
L3.push_back(s);
}
}
return L3;
}
答案 0 :(得分:0)
您的函数shuffle
要求输入,因为您从中调用了cin >> s
。 shuffle
并没有真正使用作为参数传递的两个字符串向量。实际上,它忽略了传递给它的字符串的值,形成了新输入的字符串L3。