I have a list of tasks. A task is defined by a name, a due date (and time) and a duration.
My TaskManager class handles a std::list<Task> sorted by due date. It has to provide a way to get the tasks due on a specific day.
An example : I have task 1 due on Monday 6 am, task 2 due on Monday 9 am, task 3 due on Tuesday 7 pm. If I pass “Monday” to my method, it should return tasks 1 and 2.
How would you implement that ?
I think a good way (from API point of view) would be to provide a std::list<Task>::iterator pair. So I would have a TaskManager::begin(date) method. Do you think this method should get the iterator by iterating from the start of the list until it finds the first task due on that date, or by getting it from a std::map<date, std::list<Task>::iterator> (but then we have to keep it up-to-date when adding or removing tasks) ?
And then, how could I implement the TaskManager::end(date) method ?
instead of using a
std::list, consider using astd::setorstd::map, which can keep the items in a sorted order without additional effort. Then you can find the items due on a particular date usingstd::set::lower_boundorstd::map::lower_bound.