CS1030 Dr. Mark L. Hornick - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CS1030 Dr. Mark L. Hornick

Description:

link the algorithms to the data structures. ... STL algorithms achieve generality by accessing and traversing the elements of a ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 21
Provided by: drmarkh
Category:

less

Transcript and Presenter's Notes

Title: CS1030 Dr. Mark L. Hornick


1
Standard Template Library
  • The STL is a large part of the C Standard
    Library
  • Major components
  • data structures (containers/collections for data
    CS2851)
  • vector (array), list, deque, stack, queue, set,
  • algorithms
  • Common operations (functions) that can be applied
    to many STL containers
  • iterators
  • link the algorithms to the data structures.
  • iterators are designed for efficient traversal
    through the data structures.
  • Similar to pointers

2
Linked Lists the list class
  • Arranges data in non-contiguous blocks
  • Links them together to create order

3
list is a Doubly Linked List
4
list in C (STL)
  • Syntax
  • include ltlistgt
  • using namespace std
  • listltTgt collection
  • // T datatype stored in vector

5
list Operations
  • Adding elements
  • push_back At the end
  • push_front At the beginning
  • insert Anywhere
  • Removing an element
  • pop_back From the end
  • pop_front From the beginning
  • erase, remove - Anywhere

6
list Operations (2)
  • Information accessors/inspectors
  • size How many?
  • empty Are there none?
  • Modification
  • sort NOT part of ltalgorithmgt
  • reverse change the order

7
The vector class
  • vector is a smart array
  • Like the Java ArrayList
  • Storage is in consecutive memory locations
  • Location
  • start indexsize
  • Very little storage overhead
  • Only need the start and object size

8
vectors in C (STL)
  • User-specified data type is stored
  • Syntax
  • include ltvectorgt
  • using namespace std
  • vectorltTgt collection
  • // T datatype stored in vector
  • // Like Javas ArrayListltTgt

9
vector Operations
  • Adding elements
  • push_back At the end
  • push_front At the beginning
  • insert Anywhere
  • Removing an element
  • pop_back From the end
  • pop_front From the beginning
  • erase, remove - Anywhere

10
lists and vectors usage
  • Example

11
lists vs. vectors
  • Advantages
  • Flexible storage
  • Grows easier
  • Ultimately more data can be stored
  • Disadvantages
  • O(n) access time
  • Loss of indexing
  • Storage overhead is higher

12
Iterators
  • A mechanism for sequentially accessing
  • STL containers
  • More important for list than vector
  • Vector can use indexing for access (e.g. xi)
  • Automatically included as part of the container
  • Helps describe the next and previous links

13
Iterator Notation
  • Declaration
  • listltTYPEgtiterator NAME
  • Operators
  • Access data element (dereferencing)
  • ,-- Advance or retreat in the list
  • ,! Comparison
  • NOTE No lt or gt

14
Iterators in a Linked List
Main List Cell
.begin()



.end()
15
Iterator Variations
  • Standard iterators can be used to change a list
  • iter newValue
  • Special iterators for const lists (cant change)
  • listltTYPEgtconst_iterator citer
  • Reverse iterators
  • listltTYPEgtreverse_iterator riter
  • listltTYPEgtconst_reverse_iterator
  • criter

16
More on Reverse Iterators
  • For going from end to beginning
  • Retreats
  • -- Advances
  • Bounds
  • rbegin() Last
  • rend() One before the first

17
Other iterator Member Functions
  • These member functions need iterators
  • insert Where to insert
  • erase Which one to erase
  • find Locate the item that was found
  • Iterators also apply to vectors
  • Usage is the same
  • vectorltTYPEgtiterator NAME

18
STL Algorithms
  • An algorithm is an operation (function) that can
    be applied to many STL containers
  • The STL algorithms are generic - they can operate
    on a variety of data structures.
  • STL container classes such as vector and list
  • Program-defined data structures
  • Behavior must satisfy the requirements of a
    particular algorithm
  • STL algorithms achieve generality by accessing
    and traversing the elements of a container
    indirectly through iterators.
  • Iterators must be settable and dereferencable

19
Sort Algorithm
  • The sort algorithm is an operation (function)
    that can be applied to many STL containers
  • notable exception list container (has its own
    method)
  • sort() orders a container's contents in ascending
    order
  • as defined by the operatorlt() as applied to the
    containers elements
  • Programmer-defined types can be sorted just as
    easily as a built-in type

20
find Algorithm
  • searches a subrange of the elements in a
    container (or all the elements),
  • looks for an element that is "equal to" a
    specified value stops when it finds the first
    element equal to the specified value
  • the equality operator () must be defined for
    the type of the container's elements.
  • search value must be of the same type as the
    elements stored in the container
  • or of a type that the compiler can automatically
    convert.
  • Return value is an iterator specifying the
    position of the first matching element.
  • If no matching element is found, the return value
    is equal to the iterator specifying the end of
    the element subrange
Write a Comment
User Comments (0)
About PowerShow.com