Program is in C++
Write a function named “removeInvalidDate” that accepts the vector of pointers to Reminder objects.
It will go through that list and delete the Reminder objects with an invalid date (day is not between 1 and 31 and month is not between 1 and 12) from the list.
It will return how many objects that it has deleted from the list.
Reminder class and objects are not given. This is all the question provides.
We assume that Remainder class has two member variables- day and month. We check each object pointer and vector for validity and erase the invalid ones. Here is the required code:
void removeInvalidDate(vector<Reminder*>
reminders)
{
int i=0;
while(i<reminders.size())
{
if(reminders[i]->day<1 || reminders[i]->day>31 ||
reminders[i]->month<1 || reminders[i]->month>12)
reminders.erase(reminders.begin()+i);
else
i+=1;
}
}
We use -> operators since the vector stores pointers. We do not increment i when an element is removed because the size of the vector reduces and the next element of the vector takes the place of the erased element.
Get Answers For Free
Most questions answered within 1 hours.