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

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
CIT241 Prerequisite Knowledge ◦ Variables ◦ Operators ◦ C++ Syntax ◦ Program Structure ◦ Classes  Basic Structure of a class  Concept of Data Hiding.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Overview of C++ Templates
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Lecture 7 : Intro. to STL (Standard Template Library)
Intro to the C++ STL Timmie Smith September 6, 2001.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
Associative Containers Sets Maps Section 4.8. Associative Containers.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Chapter 5 The MFC Collection Classes. How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CS212: Object Oriented Analysis and Design
18 Chapter Stacks and Queues
Vectors Holds a set of elements, like an array
Object Oriented Programming COP3330 / CGS5409
Introduction to Programming
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
An Introduction to STL.
Standard Template Library
Presentation transcript:

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 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

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

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; }

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; }

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; }

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’);

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

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;

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;

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

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

References Walter Savitch, Problem Solving With C++ The Object of Programming (2 nd Edition) Mark J. Sebern, C++ Standard Template Library SGI, Standard Template Library Programmer’s Guide

Exercises Function Templates: Binary Search Class Templates: –Linked list –Complex number (get/set operators only) STL: Linear Search Using Vector as Input