1 / 28

Week 5 - Associative Containers: sets and maps

Week 5 - Associative Containers: sets and maps. 2. Main Index. Contents. Container Types. Associative containers.

Download Presentation

Week 5 - Associative Containers: sets and maps

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. Week 5- Associative Containers:sets and maps

  2. 2 Main Index Contents Container Types

  3. Associative containers • Designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position. • A programmer can use the key without concern for how elements are physically stored and how operations make the association between the key and an element. • Insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n). Implemented using binary search trees.

  4. Sets • In a set, the data value is just the key:

  5. C++ Set • Sets are containers that store unique elements following a specific order. • The value of the elements in a setcannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container. • Multisets are containers where multiple elements can have equivalent values.

  6. Iterators • Set containers keep their elements in ascending order, which follows the container'ssorting criterion (By default, this is a lessoperator<). • Hence, begin() points at the smallest element in the set, and end() points just pastthe largestelement in the set.

  7. CLASS set <set> Operations iterator begin(); Return an iterator pointing at the first member in the set. iterator end(); Return an iterator pointing just past the last member in the set. 7 Main Index Contents

  8. set(); Create an empty set. This is the Default Constructor. CLASS set CLASS set <set> <set> Constructors Operations set(T *first, T *last); Initialize the set by using the address range [first, last). boolempty() const; Is the set empty? intsize() const; Return the number of elements in the set. 8 Main Index Contents

  9. intcount(const T& key) const; Search for key in the set and return 1 if it is in the set and 0 otherwise. CLASS set <set> Operations iterator find(const T& key); Search for key in the set and return an iterator pointing at it, or end() if it is not found. 9 Main Index Contents

  10. pair<iterator, bool> insert(const T& key); If key is not in the set, insert it and then return a pair whose first element is an iterator pointing to the new element and whose second element is true. Otherwise, return a pair whose first element is an iterator pointing at the existing element and whose second element is false. Postcondition: The set size increases by 1 if key is not in the set. CLASS set <set> Operations int erase(const T& key); If key is in the set, erase it and return 1; otherwise, return 0. Postcondition: The set size decreases by 1 if key is in the set. 10 Main Index Contents

  11. void erase(iterator pos); Erase the item pointed to by pos. Preconditions: The set is not empty, and pos points to a valid set element. Postcondition: The set size decreases by 1. CLASS set <set> Operations void erase(iterator first, iterator last); Erase the elements in the range [first, last). Precondition: The set is not empty. Postcondition: The set size decreases by the number of elements in the range. 11 Main Index Contents

  12. Set Operations

  13. Set Operations

  14. Set Operations

  15. Example

  16. Implementing Set Intersection Complexity: Up to linear O(N) at most2*(count1+count2)-1comparisons (wherecountX is the distance between firstX and lastX) C++ implementation

  17. Maps

  18. C++ Map • Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. • Thekey values are generally used to sort and uniquely identify the elements, while the mapped valuesstore the content associated to this key. • The types of key and mapped value may differ, and are grouped together in apairtype. • Multimapsare associative containers where multiple elements can have equivalent keys.

  19. Maps • A map stores an element as a <key-value> pair, • The index operator [] leads us to refer to a map as an associative array. • The index (key) for a map is not limited to integer values, but can be of any type.

  20. If k does not match the key of any element in the container, the function inserts a new element.

  21. Map iterators

  22. insert()

  23. find() and erase()

  24. sets, maps Binary search trees The storage structure should be selected so that the container operations can be implemented efficiently

  25. Reading & HW reminder • Book chapter 4.8 HW3 is due this Friday. (Submission before Thursday will receive 20 additional points)

More Related