Sets
- collection of objects
- use {} to denote sets
- no order
- no duplicate objects
Implementation in C++
#include set
// Create a set
set<int> int_set
// Insert element
int_set.insert(1)
// Get set size
int_set.size()
// Iterate through elements
set<int> :: iterator iter;
iter = int_set.begin();
while (iter != int_set.end()) {
cout << *iter;
}
// Find element
iter = int_set.find(1);
cout << (iter != int_set.end());
Belonging
- basic function of set
- use
Elements
- must limit elements of set
- don’t -> Russel’s paradox
- set is element of itself??
Empty Set
- nothing in it
- null
- special
- write as {}
Comparing
- contains:
- equals:
Power Set
The power set is the set of all subsets of a set.
Cardinality
Cardinality is just the number of elements in set.
Since the power set of has elements,
Combining Sets
Union
Union combines two sets.
Can also find partial union:
Intersection
Intersection finds the intersection of sets
Two sets and are disjoint if
Set Difference
Subtracting set from set means
Example:
Now, and are complements.
Partition
A partition is the breakdown of a set into disjoint subsets.
Cartesian Product
Gives set involving all possible pairs between sets and