Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL."— Presentation transcript:

1 COMP 171 Data 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 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 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 void Pair ::Pair(const T & x, const T & y) { first = x; second = y; } template void Pair ::setElement(int pos, const T & value) { if (pos == 1) first = value; if (pos == 2) second = value; }

8 Class Templates template const T & Pair ::getElement(int pos){ if (pos == 2) return second; return first; } Usage: Pair pair_int(1, 2); Pair 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 v_int; –vector 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 (2 nd Edition) Mark J. Sebern, C++ Standard Template Library http://www.msoe.edu/eecs/ce/courseinfo/stl/index.htm SGI, Standard Template Library Programmer’s Guide http://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


Download ppt "COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL."

Similar presentations


Ads by Google