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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Simulation. Example: A Bank Simulator We are given: –The number of tellers –The arrival time of each customer –The amount of time each customer requires.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Data Structures Using C++ 2E
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Writing Your Own STL Container Ray Lischner
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Data Structures Using C++ 2E
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Charles: A Data Structure Library for Ada95 Matthew Heaney Ada-Europe 2003 Toulouse, France.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Generic Programming Using the C++ Standard Template Library.
C++ STL CSCI 3110.
Chapter 18 Java Collections Framework
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
CDA6530: Performance Models of Computers and Networks Chapter 8: Statistical Simulation ---- Discrete Event Simulation (DES) TexPoint fonts used in EMF.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Standard Template Library
Introduction to olympic programming
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Standard Template Library (STL)
Chapter 13 Queues and Priority Queues
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Basic Data Structures.
Object Oriented Programming COP3330 / CGS5409
SystemVerilog for Verification
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Chapter 17: Linked Lists.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Iterators and STL Containers
Evaluation of List Implementations
C++ STL Stack, Queue, and Deque
CSCS-200 Data Structure and Algorithms
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

Collection types CS

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?

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

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

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 }

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)

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());

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

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

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?

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

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

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