私のremove_ifは、フィルタにかけられた要素の値でフィルタされていない要素を上書きしているようです。これらのコードの目的は、特定のカテゴリの教師のみをフィルタリングして表示できるようにすることです。 (任意の要素を削除していない) ここ私のベクトルを上書きするC++のremove_if
static string compare;
static string debug;
bool filter_Cat (Teacher &t)
{
return (t.getCat() != compare);
}
void filterCat (vector<Teacher> &t)
{
vector<Teacher>::iterator i;
vector<Teacher>::iterator newedited = remove_if(t.begin(), t.end(), filter_Cat);
for (i = t.begin(); i != newedited; ++i)
{
Teacher& te = *i;
te.getName();
cout << "\t";
te.getCategory();
cout << "\t";
te.getLocation();
}
}
void filterTutorCat(vector<Teacher> &t)
{
int choice;
cout << "No\tCategory" << endl
<< "1\tEnglish" << endl
<< "2\tMath" << endl
<< "3\tScience" << endl
<< "Choose the category you wish to filter :";
cin >> choice;
getline(cin, debug);
if(choice <= 3 && choice > 0)
{
if (choice == 1)
{
compare = "English";
filterCat(t);
}
if (choice == 2)
{
compare = "Math";
filterCat(t);
}
if (choice == 3)
{
compare = "Science";
filterCat(t);
}
}
else
{
cout << "Invalid Option" << endl;
}
}
入力と予想出力と実際の出力は何ですか? –
コードからわかるように、ユーザーはフィルタリングするカテゴリに1,2または3しか入力できません。予想される出力については、ユーザーが選択したカテゴリの教師しか表示されません。実際の出力は、「比較」と一致するカテゴリを持つオブジェクトが1つしかない場合に出力されますが、「比較」に一致するカテゴリのオブジェクトが2つある場合は、ベクトルのオブジェクトを上書きし始めます。 – delphi316