I'm trying to use remove_if to remove elements in my vector to do filtering. The problem is when I compile the coding, there were no error but when I try to use the filter function, error popped out saying I can't dereference an iterator. I have no idea what is wrong and hope you guys can help spot the problem. Here's partial of my codes
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";
}
}
remove_if returns the new end of the vector. so you should be iterating like so
From remove_if documenation
In general it is a good idea to erase the remaining elements after a remove_if
Now everything between t.begin() and t.end() is all valid and good so you can do