Sets only answer the question of membership (no duplicates).
Operations:
Comparison to Vector
Looping over Sets
#include "set.h" Set<string> friends; friends.insert("xyz"); friends.add("abc"); //prints in alphabetical order for (string myFriend : friends) { cout << "Hi " << myFriend << endl; }
Recall the common set operations (show Venn diagrams)
s.add(value) |
s.insert(value) |
O(log N) | adds an element to the set if it was not already there. |
s.clear() |
s.clear() |
O(N) | removes all elements from this set. |
s.contains(value) |
s.find(value) != s.end() |
O(log N) | returns true if value is in set. |
s.equals(set) |
s == set |
O(N) | returns true if the two sets contain exactly the same elements (number and value). |
s.first() |
*s.begin() |
O(log N) | returns the first value in the set in order. |
s.isEmpty() |
s.empty() |
O(1) | returns true if the set contains no elements. |
s.isSubsetOf(s2) |
no counterpart (can write your own subroutine) |
O(N) | returns true if all the elements in s are also present in s2. |
s.remove(value) |
s.erase(value) |
O(log N) | removes an element from this set. |
s.size() |
s.size() |
O(1) | returns the number of elements in the set. |
s.toString() |
no counterpart (can write your own subroutine) |
O(N) | converts the set to a printable string representation. |