Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Sorted, Unique Key Container

Similar presentations


Presentation on theme: "A Sorted, Unique Key Container"— Presentation transcript:

1 A Sorted, Unique Key Container
STL Sets A Sorted, Unique Key Container Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 Other member functions
iterator find(const TYPE&x) Returns the iterator or end() if(st.find(x)!=st.end()) is the same as xst iterator erase(interator N) Deletes an element, corresponds to insert Copyright © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 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 © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill

17 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 © Curt Hill

18 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 © Curt Hill

19 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 © Curt Hill

20 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 © Curt Hill

21 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 © Curt Hill

22 Operators The usual operators are allowed =, &=, |=, ^=, >>=, <<= ==, != >>, <<, |, &, ! Copyright © Curt Hill

23 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 © Curt Hill

24 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 © Curt Hill

25 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 © Curt Hill


Download ppt "A Sorted, Unique Key Container"

Similar presentations


Ads by Google