[CSUSB] >> [CompSci] >> [Dick Botting] >> [CS330 Course Materials] >> [Slides] >> 15
[Index] || [Contents] || [Grades] Mon Nov 18 16:38:35 PST 2002

Contents


    Sets

    Sets are one of the most powerful new data structures in the C++ library. They are containers that accept items and store them for you. They keep them in order and you can use iterators to access them. You can extract any one that is in the set. You can quickly (log(n)) find out if an item is already in the set.

    See chapter 12 (and bits of 13 and 14).

    Simple example [ 15set.cpp ]

    Rule 0: no duplicates in a set.

    If you want multiple occurrences.... use a 'multiset'.

    Set Abstraction

    See book...

    Set Operations in Math

  1. MATH 272
  2. Union, intersection, difference, includes...
  3. Implemented in STL <algorithms>
  4. set_union, set_intersection, set_difference, includes...
  5. A useful language for talking about the behavior of software.

    Bitsets

    Fast simple but limited size sets. Must specify how many bits. Each bit is number and either 1 or 0.

    Operations inspired by C bit operations + Pascal and Ada sets.

    Book page 267

    C and C++ bit operations: &, |, ~, and ^

    His version is close to the STL <bitset> [ bitsets ]

    application: Eratosthenes sieve.

  6. Demo using the STL set

    . . . . . . . . . ( end of section Bitsets) <<Contents | Index>>

    Using Sets


    (spell checking): Also see pages 126 and 129

    error on p273: set<int> should be set<string>


    (spelling correction): On page 274 'transpositions' should be 'transposition'

  7. and layout is weird on code in page 275.
  8. Have you tried 'ispell filename' on our machines?


    (anagrams):

    Set Operations

    Table 12.1

    STL version is very similar. [ sets ]

    Notice the reversed iterators: rbegin() and rend() for backwards through a set of items.

    p278-290 Set operations: includes, union, intersection, ...

  9. <algorithm>

    p279 Creating new sets

  10. Do not forget pp191-193.
  11. When using 'set_intersection', set_union, set_difference, set_symetric_difference from <algorith> to create a new set, use inserter.
  12. inserter(container, iterator)
  13. For example see bottom of page 279

    Set Implementation

    Uses a Binary Search Trees.

    What is a Binary Tree?

    What is a Binary Search Tree?

    UML handout shows the physical structure. [ ch12set.gif ]

    On pages 283, change 'Child.size' to 'Child->size' twice.

    A more complex node class + the iterators that traverse trees in various ways from the next chapter.

    Page 285: error in while loop: should include "&& (first != end())"

    Page 286: error in 'if(newElement'. Should be 'if(newNode->value'

    Page 288: missing 'if(result)' in front of 'result->parent'.

    Botting's law of pointer sanity: if you don't know for certain sure that pointer p points at something do not star or arrow it.

    Uses iterators from chapter 13

    Notice that the set algorithms tend to have a structure like Section 12.6.1: A Balanced Line Merge.

    Performance

    How balanced is a search tree? In the worst case a set like Budd's is a O(n) insert/find/delete structure. Can we beat this?

    You Betya! See Chapter 14... and then lookout for hashing methods.

    Summary

    Sets: A catch-all container -- easy to insert items, easy to find items, and easy to erase items.

    Study Questions

    Exercises

    . . . . . . . . . ( end of section Sets) <<Contents | Index>>

    Next

    project 3+Quiz 3
  14. based on project 3 and classes 11 thru 15 (Chapters 9.4..13)

    plus lecture on Ch14 Searching


Formulae and Definitions in Alphabetical Order