COMP 171 Data Structures and Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 171 Data Structures and Algorithms

Description:

... Problem Solving With C++ The Object of Programming ... Standard Template Library Programmer s Guide http://www.sgi.com/tech ... STL: Linear Search ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 16
Provided by: Vincen186
Category:

less

Transcript and Presenter's Notes

Title: COMP 171 Data Structures and Algorithms


1
COMP 171Data Structures and Algorithms
  • Tutorial 1
  • Template and STL

2
Function Templates
  • void swap (int x int y)
  • int temp
  • temp x
  • x y
  • y x
  • void swap (char x char y)
  • char temp
  • temp x
  • x y
  • y temp

3
Function Templates
  • Swap function for Double, for Float, for Class?
  • Function Template can HELP!
  • templateltclass Tgt
  • void swap (T x, T y)
  • T temp
  • temp x
  • x y
  • y temp
  • T can be viewed as a generic data type

4
Function Templates
  • The above is template definitions
  • Template definitions are NOT functions
  • Compiler creates functions using function
    templates
  • int x 2, y 3
  • swap(x, y)
  • char ah, bw
  • swap(a, b)
  • Two swap functions are created

5
Class Templates
  • Class Definition Pair
  • class Pair
  • public
  • Pair()
  • Pair(const int x, const int y)
  • void setElement(int pos, const int value)
  • int getElement(int pos)
  • private
  • int first
  • int second

6
Class Templates
  • Class Templates Pair
  • templateltclass Tgt
  • class Pair
  • public
  • Pair()
  • Pair(const T x, const T y)
  • void setElement(int pos, const T value)
  • const T getElement(int pos)
  • private
  • T first
  • T second

7
Class Templates
  • Defining member functions
  • templateltclass Tgt
  • void PairltTgtPair(const T x, const T y)
  • first x
  • second y
  • templateltclass Tgt
  • void PairltTgtsetElement(int pos, const T
    value)
  • if (pos 1) first value
  • if (pos 2) second value

8
Class Templates
  • templateltclass Tgt
  • const T PairltTgtgetElement(int pos)
  • if (pos 2) return second
  • return first
  • Usage
  • Pairltintgt pair_int(1, 2)
  • Pairltchargt pair_char(a, b)
  • cout ltlt pair_int.getElement(1)
  • cout ltlt pair_char.setElement(1, c)

9
STL
  • Standard Template Library
  • Contains common algorithms and data structures
  • Function templates
  • Sorting,
  • Searching, etc
  • Class templates
  • Vector
  • Stack
  • Queue
  • List, etc

10
STL Vector
  • A vector is a Sequence that supports random
    access to elements, constant time insertion and
    removal of elements at the end, and linear time
    insertion and removal of elements at the
    beginning or in the middle. The size of a vector
    can vary dynamically memory management is
    automatic.
  • vectorltintgt v_int
  • vectorltdoublegt v_double

11
STL Vector
  • Things similar to arrays
  • A sequence of elements
  • Supports random access
  • Things different from arrays
  • Dynamic size
  • Variable name of an array is a pointer
  • int a5 1, 2, 3, 4, 5
  • int i a
  • cout ltlt i

12
STL Vector
  • Some public member methods
  • size_type size() const
  • size_type max_size() const
  • bool empty() const
  • void push_back(const T x)
  • iterator insert(iterator pos, const T x)
  • iterator erase(iterator pos)
  • void clear()
  • etc

13
STL Vector
  • size_type
  • can be treated as unsigned int or long
  • iterator
  • can be treated as smart pointer
  • pointer will correctly point to next element

14
References
  • Walter Savitch, Problem Solving With C
  • The Object of Programming (2nd Edition)
  • Mark J. Sebern, C Standard Template
    Libraryhttp//www.msoe.edu/eecs/ce/courseinfo/stl
    /index.htm
  • SGI, Standard Template Library Programmers
    Guidehttp//www.sgi.com/tech/stl/

15
Exercises
  • Function Templates Binary Search
  • Class Templates
  • Linked list
  • Complex number (get/set operators only)
  • STL Linear Search Using Vector as Input
Write a Comment
User Comments (0)
About PowerShow.com