<分区>
我刚从 C 转到 C++,发现比较字符串方法有困难。 我有一个简单的任务。我需要创建一个学校教师类,然后创建一个对象数组,并在撤消所有教师之后,谁的主题类似于testSubject。
这是我的课
class Teacher
{
private:
string FamilyName;
string Name;
string Patronymic;
string sex;
int exp;
std::string subject;
string speciality;
int age;
public:
Teacher();
int getExp();
string getSubject();
int getAge();
void show();
};
这是我的功能,撤回教师列表,教授输入的主题
void ListTeacherSub (Teacher spis[], int n)
{
//List of teachers, who's subject is like testSubject
std::string testSubject;
cout<<"Enter test subject "; cin>>testSubject;
for (int i=0; i<n; i++)
{
if (spis[n].getSubject().compare(testSubject) == 0)
spis[i].show();
}
}
这里是main()函数
int main()
{
Teacher *spis;
int n;
cout<<"Enter numbers of student "; cin>>n;
spis = new Teacher[n];
for (int i=0; i<n; i++)
{
spis[i].show();
}
ListTeacherAge(spis, n);
ListTeacherEx(spis, n);
ListTeacherSub(spis, n);
delete[] spis;
return 0;
}
所以,一切都很好,但是当程序到达 ListTeacherSub(spis, n) 时它停止工作。据我所知,我过去只使用 strcmp,但不适用于字符串。 所以我决定寻找不同的实现,并找到了一个 http://www.cplusplus.com/reference/string/string/compare/
我该如何解决我的问题?