Difficulty: Intermediate
Deadline: 7 August 2004 at 19:00 GMT
Description
In set theory, the difference operator, expressed A-B, is the set of all elements of A that are not in B. For example, if A = {1,2,3,4,5} and B = {3,4,5,6,7}, A-B = {1,2}.
The symmetric difference operator of two sets A and B, expressed as A\B, is defined as (A-B) union (B-A). It consists of all the elements in A not in B as well as the elements in B not in A. Using A = {1,2,3,4,5} and B = {3,4,5,6,7} as above, A\B = {1,2,6,7}.
You will write a generic function that receives two vectors of elements and performs a symmetric difference operation on them. Your function will report results through a generic callback passed as a parameter.
Prototype
- Code: Select all
template <class Element, class Callback>
void symmetric_difference(const vector<Element>& set1, const vector<Element>& set2, Callback result_reporter);
Element will meet the following requirements:
- Copyable
- assignable
- default constructable
- operators < and == implemented
The callback will be called once for each element in the symmetric difference, like so:
- Code: Select all
Element part_of_the_answer;
// ...
result_reporter(part_of_the_answer);
Optimize for: speed
Example
Given the input: (1,2,3,4,5) and (3,4,5,6,7) where Element==int
Your function should call:
- Code: Select all
result_reporter(1);
result_reporter(2);
result_reporter(6);
result_reporter(7);
Notes
- No number will occur more than once in any input vector, but the input vectors will not necessarily be sorted.
- There is no limit on the size of the input vectors.
- You may use STL containers, but you may not use set_* functions from the STL.
- If the template parts of this contest are confusing, please ask in the thread! The contest is not difficult, and this could be a good opportunity to learn some generic programming.
Submissions
Please attach your solution as a single .cpp file (preferably named "your_screen_name.cpp") in an email to ryan@cpp-home.com, with the subject line as "Contest 41".
Post any questions or comments in this thread. Good luck and enjoy!
