set<int> s = {{...}} de-duplicates and sorts; set_intersection / set_difference / set_union all require sorted ranges and write through an inserter.set.insert(v): duplicates ignored, order kept (red-black tree, O(log n) per insert)set_intersection must be able to insert — passing both_courses.begin() will not compile, use inserter(...); (2) the direction of the difference matters — set_difference(ds, os) is "only Data Structures"; (3) all three algorithms require sorted inputs, so sort a vector first.| set | contents (ascending) | count | how |
|---|---|---|---|
| ds_students | {1001, 1002, 1005, 1008} | 4 | 6 raw entries de-duplicated (2 dropped) |
| os_students | {1001, 1003, 1008, 1009} | 4 | 5 raw entries de-duplicated (1 dropped) |
| both_courses | {1001, 1008} | 2 | intersection ds ∩ os |
| only_ds | {1002, 1005} | 2 | difference ds − os |
| all_students | {1001, 1002, 1003, 1005, 1008, 1009} | 6 | union ds ∪ os |
Check: |ds ∪ os| = |ds| + |os| − |ds ∩ os| = 4 + 4 − 2 = 6 ✓