CS240 - PowerPoint PPT Presentation

About This Presentation
Title:

CS240

Description:

– PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 14
Provided by: dickst
Category:
Tags: cs240

less

Transcript and Presenter's Notes

Title: CS240


1
CS-240
  • Container Classes

2
Definition
  • Container Class - a class that is used to hold
    collections of objects (items)
  • Bounded - has a maximum number of items that can
    be held usually based on an array as the
    container
  • Unbounded - no limit on number of items usually
    based on lists or trees

3
STLs Container Categories
  • Sequence Containers
  • vector
  • deque
  • list
  • Adapter Containers
  • stack
  • queue
  • priority queue
  • Associative Containers
  • set, multiset
  • map, multimap

4
Vector
  • Like an array
  • stores elements of like type
  • sequential indices but random access
  • Unlike an array
  • can grow larger and smaller at run time
  • only at back end
  • can have boundaries

V0
V1
V2
...
Vn-1
Room to grow
5
Lists
  • Sequential access in either direction
  • add/delete from front, back and middle

front
back
6
Stack
  • Add (push)and delete (pop) at only one end
  • Last-in-first-out (LIFO)
  • operates like a pile of cafeteria trays

7
Queue
  • Add (enqueue) at rear and remove (dequeue) from
    front end
  • First-in-first-out (FIFO)
  • like a grocery store check-out line
  • ordered chronologically (time of entry)

8
Priority Queue
  • Operates like a queue but ordering is by item
    priority rather than time of entry
  • entry is always at rear but exiting is based on
    filtering of item
  • like grocery store check-out but guy entering
    elbows his way up in line until he encounters a
    bigger guy

9
Sets
  • A collection of unique, related members
  • no ordering associated with member entry
  • associative operators
  • membership
  • union
  • intersection
  • Multiset
  • like a set but may have duplicate members

10
Maps
  • Correlates (maps) keys and values
  • example Java Hash table
  • Doesnt store data by position
  • Allows the key to index the value
  • Multimap - like a map but duplicate keys are
    allowed

11
STL Vector Class
  • include ltvectorgt
  • to allocate a vector
  • of 5 ints . vectorltintgt v15
  • of 10 doubles.vectorltdoublegt v210
  • of 6 strings.vectorltstringgt v36
  • of 8 chars, initialized with blanks.
  • vectorltchargt line(80, )
  • of 5 ints, initialized with zeros.
  • vectorltintgt v4 (0,0,0,0,0)

12
Vector Constructors
  • vector( ) - creates an empty vector
  • vector(int n, const T valueT()) - create a
    vector of n elements each having a specified
    value.
  • Vector(T first, T last) - initialize the vector
    from the address range forst to last

13
The Vector Operations
  • back( ) - returns the last element in the vector
  • push_back(const value) - add the item tp the
    end of the vector
  • pop_back( ) - removes and returns the item at the
    back
  • size( ) - returns number of elements in the
    vector
  • resize(int n, const TfillT()) - modify the
    size of the vector to n elements, if n is bigger
    new elements with value fill are added at end. If
    smaller, original values are kept for the n
    elements, rest are discarded
  • const T operator (int i )const - overloaded
    index operator
  • empty( ) - returns true if vector is empty
Write a Comment
User Comments (0)
About PowerShow.com