A Sorted, Unique Key Container STL Sets A Sorted, Unique Key Container Copyright © 2006-2017 Curt Hill
General characteristics Library is <set> or <set.h> Sorted, thus type must suitable for > or < or provide a compare object Maintains a total ordering, so you cannot change a key You must reinsert, that is delete and add Copyright © 2006-2017 Curt Hill
Declaration The template has three parameters Last two have default values set<T,COMPARE<T>,ALLOCATOR<T> > Compare is a binary function object that returns a boolean Allocator will do memory management Copyright © 2006-2017 Curt Hill
Constructors The default constructor creates the empty set A two iterator constructor creates a set with everything between the two iterators These iterators only have to be from a container that allows forward iteration A copy constructor also exists Copyright © 2006-2017 Curt Hill
Operations Assignment Comparisons Not subset at all Makes as a deep a copy as the contained class supports Comparisons Typical unusual STL compare s1 < s2 only if corresponding members are less Thus: {0, 1} <={0,2} but not {0,3} <={0,2,3} Not subset at all Copyright © 2006-2017 Curt Hill
Member functions void clear() int size() int max_size() Deletes all elements int size() Set cardinality int max_size() Maximum capacity This is a function of available memory empty() is true if the empty set Copyright © 2006-2017 Curt Hill
Other member functions iterator find(const TYPE&x) Returns the iterator or end() if(st.find(x)!=st.end()) is the same as xst iterator erase(interator N) Deletes an element, corresponds to insert Copyright © 2006-2017 Curt Hill
Insert insert(const TYPE & n) If successful Otherwise Inserts n into the set It returns a pair of items bundled together If successful The first is an iterator to the item The second is Boolean true Otherwise The first is the end iterator and false Pair is a template struct Used for bundling two things that need to be a function result Used here and other places (like maps) Copyright © 2006-2017 Curt Hill
Other Inserts iterator insert(iterator N, TYPE t) Insert value t before position N Notice that N is an iterator and not an integer The N iterator is a hint If the hint is accurate the insertion occurs in O(C) time otherwise in O(n) time Returned iterator points at the new t iterator insert(iterator first, iterator second) Inserts a sequence of items From another or same container class Copyright © 2006-2017 Curt Hill
Bounds Two methods that set an iterator Used for finding the closest iterator lower_bound(const TYPE & x) Returns an iterator to first element that is lesser or equal than x Returns the end if none exists iterator upper_bound(const TYPE & x) Returns an iterator to first element that is greater or equal than x Copyright © 2006-2017 Curt Hill
Algorithms Most of the set algorithms are not unique to the set These include: Union Intersection Difference These may require an additional include <algorithm> They may be applied to any sorted sequential containers other than set Copyright © 2006-2017 Curt Hill
Union OutputIterator set_union (InputIterator,InputIterator, InputIterator,InputIterator, OutputIterator) The first two iterators are the beginning and end of the first set, the second pair the second set and the OutputIterator is where they go The output iterator needs to be in a container suitable for output iterators Set does not allow outputiterators Copyright © 2006-2017 Curt Hill
Example // s1 = s1 s2 // All are vectors vector<int> s3; insert_iterator<set<int> > sout(s3,s3.begin()); set_union(s1.begin(),s1.end(), s2.begin(),s2.end(), sout); s1 = s3; Copyright © 2006-2017 Curt Hill
Insert Iterators An insert iterator is a form of output iterator Most iterators we have looked at are for looking at the container class An output iterator may change the class However, sets do not allow insert iterators Copyright © 2006-2017 Curt Hill
So? The set only allows const_iterators We store the output in any other STL container class In our example a vector Then put into set This shows the flexibility STL has in dealing with containers of differing organization Lets look at this one more time Copyright © 2006-2017 Curt Hill
Example vector<float> temp(set1.size() + set2.size()); vector<float>::iterator iter; iter = set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), temp.begin()); temp.resize(iter-temp.begin()); res_set = set<float>(temp.begin(), temp.end()); Copyright © 2006-2017 Curt Hill
Other set operations Intersection Difference (regular) Same form as the union but name is set_intersection Difference (regular) Same form as the union but name is set_difference The result has all those in A but not B Difference (irregular) set_symmetric_difference Produces the union of both set differences Copyright © 2006-2017 Curt Hill
Subsets There is no method for subset Construct them with union and equality A B == (A B == B && A != B) A B == (A B == B) This is not pretty using the STL operations You may be better off iterating both set simulaneously Copyright © 2006-2017 Curt Hill
STL Bags or Multisets Pretty much the same as sets with a few exceptions Still use the <set> include Class name is multiset The insert no longer returns a pair since insertion should always work Copyright © 2006-2017 Curt Hill
Bitsets in the STL An unusual STL container class It is a template class but the template is not a type but the size of the bitstring Each bitstring is fixed size Copyright © 2006-2017 Curt Hill
Constructors bitset<20> b; Creates a set that can handle zero to 19 Usually the N value should be divisible by 8 bitset<128> b(i); Converts i to a bitstring and initializes b with it Copyright © 2006-2017 Curt Hill
Operators The usual operators are allowed =, &=, |=, ^=, >>=, <<= ==, != >>, <<, |, &, ! Copyright © 2006-2017 Curt Hill
Methods any() – true if not empty none() – true if empty count() – cardinality flip() – reverse every bit reset() – sets all bits to zero reset(int) – set just this position to zero Copyright © 2006-2017 Curt Hill
More methods set() – sets all bits to one set(int) – sets just one bit to one size() – returns the template parameter to_string() – returns a string that has a 0 or 1 for each position There is a contructor that allows this to be constructed back into a set test(int) – returns true if this position is one Copyright © 2006-2017 Curt Hill
Finally The STL Set is a container class that maintains its unique items in sorted order It is easy to insert to and query The algorithms set_union, set_intersection, set_difference may be applied to any sorted container class They are very general, but not near as easy as they should be Copyright © 2006-2017 Curt Hill