Erasing Elements
In this part, you'll implement the erase method for your HashSet<T>. This method should remove the specified element from the set if it exists. If the element is not found, the method should do nothing.
A Handy Function: std::forward_list::remove
The std::forward_list class provides a remove member function that can be useful for your erase implementation. This function removes all elements equal to a specified value from the list. You can use it to remove an element from the std::forward_list in the bucket corresponding to the element's hash value.
Here's an example of how to use std::forward_list::remove:
std::forward_list<int> my_list = {1, 2, 3, 4, 5};
my_list.remove(3); // Removes all occurrences of 3 from the list
This function makes implementing erase remarkably straightforward!
Your Task
Implement the erase member function for your HashSet<T> class template. The function should have the following signature:
void erase(const T& value);
In your implementation, you should
- Compute the bucket index for the given value using the hash function.
- Call the
removemember function on thestd::forward_listin the appropriate bucket to remove the element.
When You're Done
Don't forget to add some details about what you did to Fun.md. The sample solution includes an implementation of erase, so you can leave you tests in hashset-test.cpp enabled if you'd like rather than commenting them out.
(When logged in, completion status appears here.)