我正在制作学生对象列表,并希望迭代它们并输出它们拥有的值。我不确定为什么跳过for循环。任何帮助/指导将不胜感激。这是循环:
void studentInfo(list<Student> stuList) {
cout << "in studentinfo" << endl;
for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); ++it) {
cout << "in student info loop" << endl;
cout << it->toString();
}
cout << "after loop" << endl;
}
我得到了循环之前和之后的cout消息。如果需要,这是toString()方法。
string Student::toString() {
stringstream outString;
outString << "Name: " << name << "\nID: " << id << "\nAge: " << age << endl;
return outString.str();
}
这是列表的填充方式,我通常在测试时添加2个学生对象:
void addToList(list<Student> stuList) {
string tempName;
int tempID;
int tempAge;
int numStudents;
cout << "How many students will you be entering?\n";
cin >> numStudents;
for (int i = 0; i < numStudents; i++) {
cout << "Enter student name: \n";
cin >> tempName;
cout << "Enter student id: \n";
cin >> tempID;
cout << "Enter student age: \n";
cin >> tempAge;
stuList.push_back(Student(tempName, tempID, tempAge));
}
}