我是一个c++初学者。我只想检查学生对一组答案的答案。假设我有一个学生,并像这样声明:
//Declare and array of student answers;
string student1 [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};答案关键字声明如下:
//Declare an array set of answer key
string keys [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};假设答案是从1到10的正确答案。然后我想检查学生的答案是否与声明的答案匹配:
for(const string &key : keys){
for(const string &answer : answers){
if(key == answer){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
}
}我的第一个问题是,它给了我以下结果:
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct
Correct 第二个问题是,我想再增加5个学生来检查他们的答案。谢谢。
发布于 2019-11-03 04:27:58
您可以使用for循环来实现这一点,其他人已经展示了这一点,但是您也可以使用std::transform和lambda来实现这一点,只是为了好玩。
#include <algorithm>
[...]
std::vector<string> correctness;
std::transform(student1,student1+10,keys,std::back_inserter(v),[](const string& a,const string& b){if (a==b) return "Correct"; else return "Wrong";});
for (const auto& ans : correctness) std::cout << ans << endl;https://stackoverflow.com/questions/58673099
复制相似问题