如何搜索用戶從程序中輸入的名稱?目前我有這個用於我的搜索,但每當程序找到選定的學生,它會像不斷結束的循環打印不停。即時通訊也使用多態性,因爲學生分爲本地和國際學生。使用矢量搜索名稱
我想過使用stl算法遍歷學生,但我很新的stl。我已經嘗試了一些來自互聯網的例子,但它應用於我的程序時總是給我一個錯誤。
主要功能
int main()
{
clsUniversityProgram objProgram[3];
for (int x = 0; x < 3; x++)
{
objProgram[x].programInfo();
}
vector <clsStudent*> student;
addStudents(student, objProgram);
searchStudent(student);
return 0;
}
void searchStudent(const vector <clsStudent*>& s)
{
string searchName;
const clsStudent *foundStudent;
cout << "\nEnter student name to search for. [ENTER] terminates" << endl;
cin >> searchName;
if (s.size() == 0)
cout << "There is 0 student in the database.";
while(searchName.length() != 0)
{
for (int i = 0; i < s.size(); i++)
{
if (s[i]->getName() == searchName)
{
cout << "Found " << searchName << "!";
// s[i]->print();
break;
}
else
{
cout << "No records for student: " << searchName;
cout << "\nEnter student name to search for. [ENTER] terminates" << endl;
cin >> searchName;
}
}
}
}
聽說find/find_if? http://en.cppreference.com/w/cpp/algorithm/find – billz