我想使用remove_if删除我的向量中的元素做过滤。问题是当我编译代码时,没有错误,但是当我尝试使用过滤器函数时,出现错误,说我无法取消引用迭代器。我不知道什么是错的,希望你们能帮助发现问题。 这里是我的部分代码C++关于指针/参考与remove_if
bool filter_C (Teacher &t)
{
return (t.getCat() != compare); //compare is a static string
}
void filterTeacherCategory(vector<Teacher> &t)
{
vector<Teacher>::iterator i;
Teacher *ptr;
i = remove_if(t.begin(), t.end(), filter_C);
ptr = &(*i);
for (i = t.begin(); i != t.end(); ++i)
{
ptr->getName();
cout << "\t";
ptr->getGender();
cout << "\t";
ptr->getPhone();
cout << "\t";
ptr->getCategory();
cout << "\t\t";
ptr->getLocation();
cout << "\n";
}
}
那么我还需要使用指针吗? – delphi316 2012-01-18 15:15:19
请参阅我的编辑 – 2012-01-18 15:17:50
这工作,但现在我有另一个问题。无论如何将remove_if变成像remove_ifnot之类的东西?我知道这听起来有点有趣...... – delphi316 2012-01-18 15:24:21