Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.

Similar presentations


Presentation on theme: "Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative."— Presentation transcript:

1 Week 5 - Associative Containers: sets and maps

2 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue

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. Sets The value of the elements in a set cannot 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. Multisets

6 Iterators Set containers keep their elements in ascending order, which follows the container's sorting criterion (By default, this is a less operator<). Setsorting criterionless Hence, begin() points at the smallest element in the set, and end() points just past the largest element in the set.

7 7 7 Main Index Contents CLASS 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.

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

9 9 9 Main Index Contents CLASS set Operations int count(const T& key) const; Search for key in the set and return 1 if it is in the set and 0 otherwise. 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.

10 10 Main Index Contents CLASS set Operations pair 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. 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.

11 11 Main Index Contents CLASS set Operations 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. 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.

12

13 Set Operations

14

15

16 Example

17 Implementing Set Intersection Complexity: Up to linear O(N) at most 2*(count1+count2)-1 comparisons (where countX is the distance between firstX and lastX)distance Complexity: Up to linear O(N) at most 2*(count1+count2)-1 comparisons (where countX is the distance between firstX and lastX)distance C++ implementation

18 Maps

19 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. Maps The key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in a pair type.pair Multimaps are associative containers where multiple elements can have equivalent keys. Multimaps

20 Maps A map stores an element as a 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.  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.

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

22

23 Map iterators

24

25 insert()

26 find() and erase()

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

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


Download ppt "Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative."

Similar presentations


Ads by Google