collection types cs101 2012.1. chakrabarti motivation thus far the only collection types we have...
DESCRIPTION
Chakrabarti unordered_map K is the type of the key (for us, string ) V is the type of the value (for us, int ) Declaration looks like unordered_map hist; Initially empty Can now write int hn = hist[“hello”]; If key “hello” does not exist, it will be created and value initialized to zero Can also write hist[“world”] = 5; And ++hist[“world”];TRANSCRIPT
![Page 1: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/1.jpg)
Collection types
CS101 2012.1
![Page 2: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/2.jpg)
Chakrabarti
Motivation Thus far the only collection types we have
used are vector<T> and matrix<T> Problem #1: given an input stream of
numbers between 0 and 9, maintain a histogram of counts of each number• ++hist[num]
Problem #2: given a series of (string) words from a long text, maintain a histogram of counts of each word• ++hist[word] — how to write?
![Page 3: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/3.jpg)
Chakrabarti
unordered_map<K,V> K is the type of the key (for us, string) V is the type of the value (for us, int) Declaration looks likeunordered_map<string, int> hist;
Initially empty Can now write int hn = hist[“hello”]; If key “hello” does not exist, it will be created
and value initialized to zero Can also write hist[“world”] = 5; And ++hist[“world”];
![Page 4: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/4.jpg)
Chakrabarti
Iterating over an unordered map Provides begin() and end() functions like
other collections Which have type unordered_map<K,V>::iterator
Code looks likefor (unordered_map<K,V>::iterator hx = hist.begin(); hx != hist.end(); ++hx) { … }
Inside the loop, access key as hx->first And value as hx->second
![Page 5: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/5.jpg)
Chakrabarti
Testing if a key exists hist.find(“peace”) returns an unordered_map<string, int>::iterator
If key “peace” exists in the map then we get a valid iterator from which we can access first and second
Otherwise, the returned value equals hist.end()
Will not modify hist if key not foundif (hist.find(“peace”)!=hist.end()) { // do stuff with key and value}
![Page 6: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/6.jpg)
Chakrabarti
Printing histogram sorted by words Two ways Extract all keys into a vector<K>, sort it,
iterate over it, while extracting values from hist
Use an ordered map<K,V> Feels very similar to unordered_map
except iterator runs in increasing key order Can also do key range traversal
• find(key), lower_bound(lowKey), upper_bound(highKey)
![Page 7: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/7.jpg)
Chakrabarti
Extracting the minimum element map<K,V> myMap; If myMap is non-empty, myMap.begin()
points to the entry with the smallest key After recording the key and value, can
remove it using myMap.erase(myMap.begin());
![Page 8: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/8.jpg)
Chakrabarti
multimap<K, V> unordered_map<K,V> and map<K,V> allow
at most one entry for each distinct key This is not a loss of generality, because you
could always declaremap<string, vector<int>> myMultiMap;
But for convenience, C++ also provides multimap<K,V>
Does not provide hist[word] because it is not clear which entry you mean
Access via iterator and pair<K,V> only
![Page 9: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/9.jpg)
Chakrabarti
list<T>
Like a train but more flexible in some ways Unlike vector<T>, no access by index Only access by iterator Can delete and insert item at iterator in
constant time front, push_front, pop_front back, push_back, pop_back
a b c e
begin endd
![Page 10: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/10.jpg)
Chakrabarti
A queue simulation problem Customers arrive at a queue If many queues, join the shortest one Arrival time between successive customers
follows some distribution Service time at the queue follows some
distribution Exponential distribution is often used
How many toll lanes? Typical delay?
![Page 11: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/11.jpg)
Chakrabarti
Event simulation Events happen at discrete points in time 0th customer C0 arrives at time 0 Triggers two events in future
• 0th customer C0 leaves queue after service• 1th customer C1 arrives
multimap<Time,??> registers events
0
C0 arrives
C0’s service time
C0 leaves
Inter-arrival time
C1 arrives
C1 waits
C1’s service time
C1 leaves
![Page 12: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/12.jpg)
Chakrabarti
Event simulation loop Remove next event from event map If event is customer arrival
• Assign customer ID, record arrival time• Push customer to back of queue• Generate random next customer arrival time• Record next customer arrival time in event map
If event is customer service completion• Pop customer from front of queue• Collect statistics• Generate random completion time for next
customer and record in event mapIf this customer is at the front of queue, can start
service immediately
![Page 13: Collection types CS101 2012.1. Chakrabarti Motivation Thus far the only collection types we have used are vector and matrix Problem #1: given an input](https://reader036.vdocuments.us/reader036/viewer/2022082602/5a4d1b8b7f8b9ab0599bed66/html5/thumbnails/13.jpg)
Chakrabarti
Event simulation data structures
Customer at front ofqueue, being served
pop_front
list<Customer> queue
multimap<Time, Action> events
Next event What will happen
For each customer:• Customer ticket number• Arrival time, start service time, end service time
struct
push_back