containers - classifications

Upload: sivaraman-p-s

Post on 01-Mar-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Containers - Classifications

    1/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Overview of Containers

    Standard Template Language (STL) organizes its classes into categories

    Sequence Containers !rra"s and #ectors (pro$ides inde% access to data)

    List& se'uence

    De'ue (can mo$e either end ut not middle)

    Adapter Containers Stac*s (last in first out)

    ueue (first in first out)

    ,riorit" ueue (deletion returns the largest&smallest $alue

    Associative Containers Set and Multiset& ag (elements are the information - ordering is " content)

    Map& Multimap (*e"data relationship/ associati$e pairs)

    !ll the container classes are templated and what the container does and howit wor*s is independent of elements it contains

  • 7/25/2019 Containers - Classifications

    2/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    !at are Containers"

    ! Containeris a data structure whose main purpose is to store and retrie$e

    a large numer of $alues

    Containers are astract data t"pes (!DTs) that hold $alues. The" don0t

    change the content - onl" hold it so that it can e retrie$ed later.

    These data structure permits storage and retrie$al of data itemsindependent of content. The two fundamental operations of an" container

    are1

    Put(C,x)1 2nsert a new data itemxinto the container C.

    Get(C)1 3etrie$e the ne%t item from the container C.

    Different t"pes of containers support different retrie$al orders ased on insertion

    order or position.

    2tems in Containers are referred to " special o4ects called1 iterators.

  • 7/25/2019 Containers - Classifications

    3/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    !at are Containers"

    The STL consists of 56 container classes categorized according to the

    ordering of the elements and the different t"pes of operations that accessthe data

    Se'uence Containers1 store data " position in linear order 5st 7nd rd etc.

    !ssociati$e Containers1 store elements " *e" e.g. name ssn part numers etc.

    ! program accesses an element in an associati$e container " its *e" which

    ma" ear no relationship to the location of the element in the container

    !daptor Containers1 contain other containers as their underl"ing storage

    structure.

  • 7/25/2019 Containers - Classifications

    4/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Containers

    Se'uence Containers

    !dapter Containers

    !ssociati$e Containers

    #ector Stac* Set Multiset

    De'ue ueue Map Mutltimap

    List ,riorit" ueueTrees

    8ash

  • 7/25/2019 Containers - Classifications

    5/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Sequence Containers #ector

    9e tal*ed aout this alread"::

    ! $ector is a generalized arra" thatstores a collection of elements of thesame data t"pe

    Similar to an arra" - a $ector allowsaccess to its elements " using aninde% in the range from 6 to n5 wheren is the size of the $ector

    ;nli*e an arra" - a $ector hasoperations that allow collection of the

    elements to grow d"namicall" at therear of the se'uence to meet theruntime needs of an application

    !llows direct access to its elementsthrough the inde%

    7 4 9

    7 4 9 3 1 0 0 0

    7 4 9 3

    v e c t o r v ( w i t h 5 e l e m e n t s )

    v . r e s i z e ( 8 ) ; ( g r o w t o 8 e l e m e n t s )

    1

    v . r e s i z e ( 3 ) ; ( s h r i n k t o 3 e l e m e n t s )

    !at is t!e disadvanta$e of insertion

    or deletion wit!in t!e interior of t!e

    sequence""

  • 7/25/2019 Containers - Classifications

    6/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Sequence Containers % Lists

    ! list is a data structure that stores elements " position

  • 7/25/2019 Containers - Classifications

    7/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Associative Containers Stac(s ) *ueues

    !ssociati$e containers store elements " *e" - in what application is this useful:: ). =2=> ma" seem the fairestwa" to control waiting times. 8owe$er for man" applications data items ha$e infinitepatience. ueues are tric*ier to implement than stac*s and are appropriate onl" forapplications (li*e certain simulations) where the order is important. Theputand getoperations for 'ueues are usuall" called enqueueand dequeue.

    Stac*s ad ueues ca $e implemented using either arra"s or lin*ed lists

  • 7/25/2019 Containers - Classifications

    8/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Associative Containers Stac(s ) *ueues

    ?oth stac*s and 'ueues are storage containers that restrict how elements

    enter and lea$e a se'uence

  • 7/25/2019 Containers - Classifications

    9/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Stac( Containers

    ! stac* allows access at onl" one end of the se'uence called the top

    &

    " t o '

    s h "

    !

    "

    t o '

    s h !

    !t o '

    s h &

    ( $ )

    &

    !

    "

    t o '

    o ' &

    ( * )

    !

    " t o '

    o ' !

    "

  • 7/25/2019 Containers - Classifications

    10/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    *ueue Containers

    ! 'ueue is a container that allows access onl" at the

    front and rear of the se'uence.

    " ! & +

    ,

    ! & + ,

    "

    r e $ r f r o n t

    # n s e r t + e l e t e

  • 7/25/2019 Containers - Classifications

    11/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    'riorit+ *ueue Containers

    ! priorit" 'ueue is a storage structure that has restricted

    access operations similar to a stac* or 'ueue.ncein the container a delete operation remo$es the largest

    (or smallest) $alue.

    1 8

    3

    1 3

    1 5

    - $ l e 8

    7

  • 7/25/2019 Containers - Classifications

    12/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Associative Containers ,aps- ,ulti.aps

    ! set is a collection of uni'ue $alues called *e"s or set memers.

    5

    3

    1

    1 5 7

    / e t "

    o r

    ! i c k

    2 o n $

    / e t !

    !

    e e '

    $ g $ r

  • 7/25/2019 Containers - Classifications

    13/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Associative Containers Sets- ,ultisets

    ! map is a storage structure that implements a *e"$alue relationship.

    + 7 ! 9 1 6

    9 1 " 8 3

    4 . 9 5

    1 . 5 0

    i r $ g e

    & $ l l o w $

    " 9 4 6 8

    + 7 ! 9 1 6

    9 1 " 8 3

    # n e - e n o r r i c e $ r t

    " 9 4 6 8 8 . 7 5 $ r t i n

  • 7/25/2019 Containers - Classifications

    14/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Stac(s

    =urther Stac* !nalogies

    ,ushing& ,opping a Stac*

    Class Stac*

    Mutliase

    ;ncoupling Stac*

    D S CSCI 362 O i f STL C i Cl

  • 7/25/2019 Containers - Classifications

    15/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Stac(s

    ! se'uence of items which are accessile onl" at the top

    D S CSCI 362 O i f ST C i ClD t St t CSCI 362 O i f STL C t i Cl

  • 7/25/2019 Containers - Classifications

    16/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    Stac( % LI/O

    Since pop remo$es the item last pushed into the stac* - it is a Last 2n =irst out

    L2=> ordering container

    !

    ,ush !

    !

    D

    ,ush D

    !

    ?

    C

    ,ush C

    !

    ?

    ,ush ?

    !

    ,op ?

    !

    ,op C

    ?

    D t St t CSCI 362 O i f STL C t i ClD t St t CSCI 362 O i f STL C t i Cl

  • 7/25/2019 Containers - Classifications

    17/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    CLASS Stac(

    CLASS stack Constructor

    stac(()/

    Create an empt" stac*CLASS stack Operations

    oole.pt+()/ const

    Chec* whether the stac* is empt". 3eturn true if it is

    empt" and false otherwise.

    D t St t CSCI 362 O i f STL C t i ClD t St t CSCI 362 O i f STL C t i Cl

  • 7/25/2019 Containers - Classifications

    18/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    CLASS stack Operations

    $oid pop()/

    3emo$e the item from the top of the stac*.,recondition1 The stac* is not empt".,ostcondition1

  • 7/25/2019 Containers - Classifications

    19/34Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    CLASS stack Operations

    int si0e() const/

    3eturn the numer of items on the stac*.

    T@ top() const/3eturn a reference to the $alue of the item at the

    top of the stac*.

    ,recondition1 The stac* is not empt".

    const T@ top() const/Constant $ersion of top().

    CLASS Stac(

    D t St t CSCI 362 O i f STL C t i ClData Structures: CSCI 362 Overview of STL Container Classes

  • 7/25/2019 Containers - Classifications

    20/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    1sin$ a Stac( to Create a e 4u.5er

    : 1 :

    : " :

    : :

    : " :

    : :: :

    4 3 1 ; 1 6 1 5

    4 3 1 < 1 6 6 6 ; 1 6 1 0

    6 < 1 6 1

    1 ; 1 6 1

    1 < 1 6 0

    s h + i g i t & h $ r $ c t e r s

    : 1 :

    : " :

    : :

    : " :

    : : : :

    o ' : 1 :

    n m / t r = 1 =

    o ' + i g i t & h $ r $ c t e r s

    o ' : " :

    n m / t r = 1 " = o ' : :

    n m / t r = 1 " =

    efer to ,ulti%5ase pro$ra. 7%&8 pa$e 339

    = =

    !

    5

    =

    !

    =

    !

    5

    =

    !

    =

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

  • 7/25/2019 Containers - Classifications

    21/34

    Dr. Nazli Mollah

    Data Structures: CSCI 362 Overview of STL Container ClassesData Structures: CSCI 362 Overview of STL Container Classes

    lecture notes adapted from

    Data Structures with C++ using STL

    1ncouplin$ Stac( le.ents

    > r $ i n ! e f o r e ? n c o ' l i n g ," ! & + ,

    efer to code on pa$e 337 usin$ ;tar$et