There are some useful algorithms that run on two sets of values
Here's a graphical interpretation of how they work, and it is how I remember them:
Remember, that these algorithms work for any sorted container, ie
Therefore, we should consider them as boolean operations on sets.
Consider two sets, A and B:
A: { 1, 2, 3, 4, 5, 6 }
B: { 4, 5, 6, 7, 8 }
vector<int> A = { 1, 2, 3, 4, 5, 6 };
vector<int> B = { 4, 5, 6, 7, 8 };
vector<int> result;
B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3 }
std::set_difference( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3, 7, 8 }
std::set_symmetric_difference( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
Notes: the sorting criterion used is operator< or a function object which you can provide.
- set_union
- set_intersection
- set_difference
- set_symmetric_difference
Here's a graphical interpretation of how they work, and it is how I remember them:
Remember, that these algorithms work for any sorted container, ie
- a sorted array
- a sorted vector
- a sorted deque
- a sorted list,
- a set
Therefore, we should consider them as boolean operations on sets.
Consider two sets, A and B:
A: { 1, 2, 3, 4, 5, 6 }
B: { 4, 5, 6, 7, 8 }
vector<int> A = { 1, 2, 3, 4, 5, 6 };
vector<int> B = { 4, 5, 6, 7, 8 };
vector<int> result;
set_union( A, B ) or A | B
A: { 1, 2, 3, 4, 5, 6 }
B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3, 4, 5, 6, 7, 8 }
std::set_union( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3, 4, 5, 6, 7, 8 }
std::set_union( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
set_intersection( A, B ) or A.B
A: { 1, 2, 3, 4, 5, 6 }
B: { 4, 5, 6, 7, 8 }
result = { 4, 5, 6 }
std::set_intersection( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
B: { 4, 5, 6, 7, 8 }
result = { 4, 5, 6 }
std::set_intersection( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
set_difference( A, B ) or A - B
A: { 1, 2, 3, 4, 5, 6 }B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3 }
std::set_difference( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
set_symmetric_difference( A, B ) or (A-B) | (B-A)
A: { 1, 2, 3, 4, 5, 6 }B: { 4, 5, 6, 7, 8 }
result = { 1, 2, 3, 7, 8 }
std::set_symmetric_difference( A.begin(), A.end(),
B.begin(), B.end(),
std::back_inserter(result) );
Notes: the sorting criterion used is operator< or a function object which you can provide.
In many cases, it is useful.
ReplyDeleteEspecially, when we use an already sorted containers ( like as: map or set).
But, keep in mind "An unordered_map is, as the name implies, inherently not sorted or sortable in-place."!!!