1 / 15

COMP 171 Data Structures and Algorithms

COMP 171 Data Structures and Algorithms. Tutorial 1 Template and STL. 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; }. Function Templates.

travis
Download Presentation

COMP 171 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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! template<class T> 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 a=‘h’, b=‘w’; 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 template<class T> 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: template<class T> void Pair<T>::Pair(const T& x, const T& y) { first = x; second = y; } template<class T> void Pair<T>::setElement(int pos, const T & value) { if (pos == 1) first = value; if (pos == 2) second = value; }

  8. Class Templates template<class T> const T &Pair<T>::getElement(int pos){ if (pos == 2) return second; return first; } Usage: • Pair<int> pair_int(1, 2); • Pair<char> pair_char(‘a’, ‘b’); • cout << pair_int.getElement(1); • cout << 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. • vector<int> v_int; • vector<double> 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 a[5] = {1, 2, 3, 4, 5}; int * i = a; cout << *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 Programmer’s 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

More Related