1 / 9

CSCI 383

CSCI 383. Object-Oriented Programming & Design Lecture 25 Martin van Bommel. Associative Containers. Provide direct access to store and retrieve elements via keys (often called search keys ) Each associate container maintains its keys in sorted order

mendozar
Download Presentation

CSCI 383

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. CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel

  2. Associative Containers • Provide direct access to store and retrieve elements via keys (often called search keys) • Each associate container maintains its keys in sorted order • Implemented as a (self-balancing) Binary Search Tree (BST) • Time complexity of Find, Insert, Delete? • The ordering of the elements is determined by a comparator function object • Default is less<T> • If f is an object of class less<T> and x and y are objects of class T, then f(x,y) returns true if x < y and false otherwise • If T is a user-defined type (e.g., Student), must provide overloading of operator<

  3. Associative Containers • The four associative containers are • set, multiset • map, multimap • Iterating through an associative container traverses it in the sort order for that container

  4. Set & Multiset • Simple associative containers, meaning that elements are their own keys (i.e., a key is not associated with any additional value) • Maintain keys in sorted order • Provide set operations • Union • Intersection • Difference • Symmetric difference • Handout #15

  5. Map & Multimap • Pair associate containers, meaning that its value type is a pair (often called key/value pair) • Duplicate keys are allowed in a multimap, so multiple values can be associated with a single key (i.e., a one-to-many relationship) • Duplicate keys are not allowed in a map, a single value can be associated with each key (i.e., a one-to-one mapping) • Also known as an associative array, providing key in map’s subscript operator[] locates value associated with key • Handout #15

  6. Container Adaptors • They are not containers, but adapt other containers • Do not provide the actual data structure implementation in which elements can be stored • Provide restricted subset of container functionality • Do not support iterators • A benefit of an adaptor class is that you can choose an appropriate underlying data structure • A stack is implemented with a deque by default but this can be changed with a second argument in constructor, e.g., vector<T> instead of deque<T>

  7. Adaptor Containers • The STL provides three adaptor containers • stack • queue • priority_queue • All three adaptor classes provide member functions push, pop and top that properly insert an element, remove an element, and check the “top” element (some implementations of queue use front to get a reference to the “first” element) • Handout #16

  8. bitset • A special container designed to store bits (i.e., elements with only two possible values: 0 or 1, true or false, ...) • Very useful for representing a set of bit flags • Very similar to a regular array, but optimizing for space allocation • Each element occupies only one bit, which is eight times less than the smallest elemental type in C++ (i.e., char) • bitsets are fixed in size at compile time (i.e., unlike vector which allows for dynamic resizing)

  9. Sieve of Eratosthenes • A simple and ancient algorithm for finding all prime numbers up to a specified integer • Created by Eratosthenes, an ancient Greek mathematician • ALGORITHM (Done in class)

More Related