stl: maps jyh-shing roger jang ( 張智星 ) csie dept, national taiwan university

5
STL: Maps Jyh-Shing Roger Jang ( 張張張 ) CSIE Dept, National Taiwan University

Upload: morgan-walsh

Post on 17-Jan-2018

241 views

Category:

Documents


0 download

DESCRIPTION

3 Examples of Maps Examples

TRANSCRIPT

Page 1: STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

STL: Maps

Jyh-Shing Roger Jang (張智星 )CSIE Dept, National Taiwan University

Page 2: STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

2

Intro to Maps

STL map class An easy way to create “associative array” of key-data pairs,

for instance: Dictionary key: word, data: word’s definition Yellow page key: name, data: phone number

Can be viewed as a vector indexed by keys of strings Keys could be strings or numbers

Alternatively, we may want to create hash tables, which involves more hassles To create a good hash function To handle collisions

Aka dictionary

Page 3: STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

3

Examples of Maps

Examples

Page 4: STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

4

Summary

The Good Maps provide a way of using “associative arrays” that allow you

to store data indexed by keys of any type Maps can be accessed using iterators with two members

“first” corresponds to the key “second” is the value associated with the key

Maps are fast to have O(log(n)) time for insertion and lookup. Internally, the elements in a map are sorted from lower to

higher keys The gotchas

Lookup in a map requires using the find function. You need to use multimap if a key is associated with multiple

pieces of data.

Page 5: STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

5

References

References http://www.cprogramming.com/tutorial/stl/stlmap.html: A

basic tutorial http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html:

More working examples