1 / 12

Map and Set ADTs

Map and Set ADTs. Joe Meehean. Conceptual Pictures. List of names Set of names Map names as keys phone #’ s as values. [Phil, Will, Bill]. Phil. Phil. Will. Will. Bill. Bill. Phil: 555-5699. Phil. Will: 555-0111. Will. Bill. Bill: 555-9864. Sets.

burt
Télécharger la présentation

Map and Set ADTs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Map and Set ADTs Joe Meehean

  2. Conceptual Pictures • List of names • Set of names • Map • names as keys • phone #’s as values [Phil, Will, Bill] Phil Phil Will Will Bill Bill Phil: 555-5699 Phil Will: 555-0111 Will Bill Bill: 555-9864

  3. Sets • Unorded collection of itemswithout duplicates • List implementation • add => O(N): check for uniqueness • contains => O(N) • bad idea • Balanced tree implementation • much better (we’ll see more about this later)

  4. C++ STL Sets set<Key, Compare, Alloc> Key is the data type being stored Compare is a comparator Ignore Alloc (almost never needed)

  5. C++ STL Set Methods • pair<iterator,bool> insert(const T &key) • inserts the key • bool is true if key not already in set • size_type erase(const T &key) • removes the item from the set • iterator find(const T &key) • returns iterator to key • iterator begin(), end()

  6. Sets vs Lists • Both store collections of objects • Lists are ordered, sets are not • items have a position in a list • positions are meaningless in sets • Duplicates • list can contain duplicates • sets, no way

  7. Sets vs Lists • Implementation • lists: array or linked list • sets are balanced trees or hash tables • Some operations are faster in a set • remove • contains • Use a set when… • don’t have/want duplicates • position does not matter • need fast contains and remove

  8. Maps • An unorded collection of unique keys with associated values • no duplicate keys allowed • Generalization of an array • arrays can only be indexed by ordinal data types • maps can be indexed with any comparable type • What is ordinal data types? • type with 1-to-1 mapping integers

  9. Map Terminology • Maps • verb: a key is associated with stated value • e.g., B maps to 2 • symbol: B => 2 • Mapping • noun: key-value pair • symbol: B => 2 • Associative array, dictionary • other words for map (n)

  10. C++ STL Maps • map<Key, Data, Compare, Alloc> • Key is the data type of the key • Data is the data type of the value • Compare is a comparison functor • Ignore Alloc (almost never needed)

  11. C++ STL Maps Methods • Map methods • Overloads the [ ] operator • Insert • map[“Bill”] = 27 • Access • cout << map[“Bill”] << endl; • Erase • map.erase(“Bill”)

  12. Questions?

More Related