Presentation is loading. Please wait.

Presentation is loading. Please wait.

A dictionary lookup mechanism

Similar presentations


Presentation on theme: "A dictionary lookup mechanism"— Presentation transcript:

1 A dictionary lookup mechanism
The STL Map A dictionary lookup mechanism Copyright © Curt Hill

2 Forward This will have something in common with previous container classes Such as the vector and list Reflects the consistent design of the STL We do not need to know, but this is a tree Copyright © Curt Hill

3 Library Maps have a similar include as lists and vectors
Like a list and vector either #include <map.h> Only available on older compilers or #include <map> using namespace std; Copyright © Curt Hill

4 Contrast with previous
Unlike a list or vector each map must have two types One is for the key and another for the data The list had no key, data only The vector had a positional key, but stored data only The key must be suitable for comparisons and copying, the data only copying Copyright © Curt Hill

5 Other Differences Since the map is inherently sorted by the key, the key cannot be changed Keys may be inserted or removed, but not changed However, the data a key identifies may be changed Copyright © Curt Hill

6 Pairs Since the data is a key+data pair we have a new datatype: the pair The pair is like a open class or struct with two members: first and second first is the key second is the data An iterator will now point at a pair Thus if iter is a suitable iterator then iter->first accesses the key and iter->second accesses the data Some methods will return a pair as well Copyright © Curt Hill

7 Constructors Default: map<key_type, data_type> id;
This form requires a default constructor for both types Primitives are OK Example: map<int, wxString> id; Copy: map<key_type, data_type > mp(map< key_type, data_type>); Copyright © Curt Hill

8 Methods and Operators Assignment
May use the = operator Erases the entire map and copies in the new one The subscripting brackets may be used with maps but they are different than the vector Copyright © Curt Hill

9 Reference The contents of the brackets is the key type So if we have:
map<double,int> mp; Then: int i = mp[3.5]; is quite acceptable The double is not truncated to an int but is the key of a lookup The search produces an integer Copyright © Curt Hill

10 More on brackets The reference may occur on either side of the assignment operator If the reference is on the left If the item does not exist then it is an insertion otherwise a replacement The thing on the RHS and in brackets must be compatible with the data and key types respectively If the reference is on the right If it exists, then same as a find except thing on LHS must be compatible with data, not a pair If it is not found, then it creates and inserts it with a default constructor for the data type Not as good as a find method Copyright © Curt Hill

11 Iterators These are mostly similar to other container class iterators
However they are pointers at pairs Example: map<double,int> mp; map<double,int>::iterator it; for(it=mp.begin(); it!=mp.end(); it++){ double d = it->first; int i = it->second; } Copyright © Curt Hill

12 Other member functions
void clear() Deletes all elements int size() Returns the number of elements int max_size() Maximum capacity This is a function of available memory bool empty(); Copyright © Curt Hill

13 Find iterator find(key_type)
Returns an iterator to the item if found and end() otherwise This is used when a created item is not wanted Thus: c = mymap[key]; Will create the item if it is not found Thus it cannot fail Copyright © Curt Hill

14 Erase int erase(key_type N) void erase(it1, it2) Deletes an element
Returns 1 if deleted and zero otherwise void erase(it1, it2) Deletes a range of element Both iterators must point into same map The second must be reachable from the first Copyright © Curt Hill

15 Insert We may use the brackets to add an item
We may also use insert: iterator insert(pair) The pair is constructed from key and data: pair<KEY, DATA>(k,d) Copyright © Curt Hill

16 More methods iterator insert(iterator N, KEY t)
Insert value t before position N Returned iterator points at the new t Default constructor used for data There are several other inserts as well Using this and iteration an At function may be implemented iterator find(key_type) Returns an iterator to the item if found and end() otherwise Copyright © Curt Hill

17 Comparisons Maps have the following operators overloaded:
==, !=, <, >, >=, <= The < is true if every element is less than corresponding element in other Copyright © Curt Hill

18 Map and list algorithms
Several list algorithms are missing from maps void reverse() The sort structure is fixed There are reverse iterators of type reverse_iterator The rbegin() and rend() give you the end points Copyright © Curt Hill

19 Other missing algorithms
void sort() A map is inherently sorted, you cannot make it unsorted void unique() The map is inherently a set of unique elements However there are multimaps Copyright © Curt Hill

20 Multimaps Similar to maps but with non-unique keys
Brackets are not allowed because the key is not unique Use find to obtain the key and iterators to move to keys that are the same Most other methods work Copyright © Curt Hill


Download ppt "A dictionary lookup mechanism"

Similar presentations


Ads by Google