Welcome to today's lesson! We'll be exploring a practical application of C++ sets by managing student enrollments for various courses. Imagine you're running an online course platform and need to handle enrollments, checks, and listings of students in different courses. std::unordered_set
would be perfect for this kind of problem since it does not allow duplicates, ensuring that a student can't enroll in the same course more than once!
By the end of this session, you'll be well-versed in using sets for such tasks. Let’s dive in!
Here are the methods we need to implement in our enrollment system:
void enroll(const std::string& student, const std::string& course);
: This method adds a student to a course. If the student is already enrolled, it does nothing.bool unenroll(const std::string& student, const std::string& course);
: This method removes a student from a course. It returnstrue
if the student was enrolled and has now been removed. Otherwise, it returnsfalse
. If after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.bool is_enrolled(const std::string& student, const std::string& course);
: This method checks if a student is enrolled in a course. It returnstrue
if the student is enrolled and otherwise.
