Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Collection types CS101 2012.1

2 Chakrabarti Motivation  Thus far the only collection types we have used are vector and matrix  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?

3 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”];

4 Chakrabarti Iterating over an unordered map  Provides begin() and end() functions like other collections  Which have type unordered_map ::iterator  Code looks like for (unordered_map ::iterator hx = hist.begin(); hx != hist.end(); ++hx) { … }  Inside the loop, access key as hx->first  And value as hx->second

5 Chakrabarti Testing if a key exists  hist.find(“peace”) returns an unordered_map ::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 found if (hist.find(“peace”)!=hist.end()) { // do stuff with key and value }

6 Chakrabarti Printing histogram sorted by words  Two ways  Extract all keys into a vector, sort it, iterate over it, while extracting values from hist  Use an ordered map  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)

7 Chakrabarti Extracting the minimum element  map 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());

8 Chakrabarti multimap  unordered_map and map allow at most one entry for each distinct key  This is not a loss of generality, because you could always declare map > myMultiMap;  But for convenience, C++ also provides multimap  Does not provide hist[word] because it is not clear which entry you mean  Access via iterator and pair only

9 Chakrabarti list  Like a train but more flexible in some ways  Unlike vector, 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 abce begin end d

10 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?

11 Chakrabarti Event simulation  Events happen at discrete points in time  0 th customer C0 arrives at time 0  Triggers two events in future 0 th customer C0 leaves queue after service 1 th customer C1 arrives  multimap registers events 0 C0 arrives C0’s service time C0 leaves Inter-arrival time C1 arrives C1 waits C1’s service time C1 leaves

12 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 map If this customer is at the front of queue, can start service immediately

13 Chakrabarti Event simulation data structures Customer at front of queue, being served pop_front list queue multimap events Next eventWhat will happen For each customer: Customer ticket number Arrival time, start service time, end service time struct push_back


Download ppt "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."

Similar presentations


Ads by Google