Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)

Similar presentations


Presentation on theme: "Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)"— Presentation transcript:

1

2 Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan) (2345, Charlie) Etc.

3 Dictionaries (theKey, theElement) (1234, Nan) (2345, Lauren) Operations.  find(theKey) find the pair with a specified key  erase(theKey) delete the pair with a specified key  insert(theKey, theElement)

4 Application Collection of student records in this class.  (key, element) = (student name, linear list of assignment and exam scores)  All keys are distinct.  Elements are in ascending order of key Get the element whose key is Stephen. Update the element whose key is Garrick.  insert(Key, element) implemented as update when there is already a pair with the given key. If a pair with the given key exists, return false.  erase() followed by insert().

5 Application Collection of student records in this class.  (key, element) = (student name, linear list of assignment and exam scores)  All keys are distinct.  Elements are in ascending order of key Get the element whose key is Stephen. Update the element whose key is Garrick.  insert(Key, element) implemented as update when there is already a pair with the given key. If a pair with the given key exists, return false.  erase() followed by insert().

6 Dictionary With Duplicates Keys are not required to be distinct. Word dictionary.  Pairs are of the form (word, meaning).  May have two or more entries for the same word. (bolt, a threaded pin) (bolt, a crash of thunder) (bolt, to shoot forth suddenly) (bolt, a gulp) (bolt, a standard roll of cloth) etc.

7 Pair template struct pair { T1 first; T2 second; // constructer pair() : first(T1()), second(T2()) {} pair(const T1& x, const T2& y) : first(x), second(y) {} // copy constructer template pair (const pair &p) : first(p.first), second(p.second) { } }

8 example

9 Make_pair template pair make_pair (T1 x, T2 y) { return ( pair (x,y) ); }

10

11 Represent As A Linear List L = (e 0, e 1, e 2, e 3, …, e n-1 ) Each e i is a pair (key, element). 5-pair dictionary D = (a, b, c, d, e).  a = (aKey, aElement), b = (bKey, bElement), etc. Array or linked representation.

12 Array Representation abcde Sorted Array ABCDE elements are in ascending order of key 5-pair dictionary D = (a, b, c, d, e). a = (aKey, aElement), b = (bKey, bElement), etc.

13 Unsorted Chain abcde NULL firstNode Sorted Chain ABCDE NULL firstNode Elements are in ascending order of Key.

14 Lab 3 SortedArray derived from dictionary will be provided sortedChain (interface) derived from dictionary will be provided Students will be asked to implement the member functions of sortedChain. Test program will be provided

15 C++ STL map Unique key values: no two elements in the map have keys that compare equal to each other. A similar associative container allowing for multiple elements with equivalent keys, multimap. multimap

16 C++ map Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value. the key value is generally used to uniquely identify the element, while the mapped value is some sort of value associated to this key. Types of key and mapped value may differ

17 C++ map

18

19

20 constructor Element access: operator[]

21 Operator =

22 begin() & end()

23 empty()

24 size()

25 []

26 Insert()

27 Erase()

28 Swap()

29 Clear()

30 Find()

31 Count() a is an element of mymap. b is not an element of mymap. c is an element of mymap. d is not an element of mymap. e is not an element of mymap. f is an element of mymap. g is not an element of mymap.

32 lower_bound() and upper_bound() a => 20 e => 100

33 Equal_range()

34 multimap http://www.cplusplus.com/reference/stl/mul timap/


Download ppt "Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)"

Similar presentations


Ads by Google