CSCI 104 Templates. Mark Redekopp David Kempe

Similar documents
CSCI 104 Templates. Mark Redekopp David Kempe

CSCI 104 Classes. Mark Redekopp David Kempe

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

CS 103 Unit 12 Slides

CSCI 104 Polymorphism. Mark Redekopp David Kempe

CSCI 104 Abstract Data Types. Mark Redekopp David Kempe

CSCI 104 Abstract Data Types. Mark Redekopp David Kempe

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

EE 355 Unit 13 C++ STL; Iterators, Maps, Sets. Mark Redekopp

Comp151. Function Templates & Class Templates

CSCI Trees. Mark Redekopp David Kempe

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

CSCI 104 Inheritance. Mark Redekopp David Kempe

Programming, numerics and optimization

Linked List using a Sentinel

pointers & references

Review: C++ Basic Concepts. Dr. Yingwu Zhu

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

CSCI 104 C++ STL; Iterators, Maps, Sets. Mark Redekopp David Kempe

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Object Oriented Design

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Cpt S 122 Data Structures. Templates

C++ Modern and Lucid C++ for Professional Programmers

boost::enable_if Deep Down Boostimagical Fun Christian Bay and Kasper A Andersen Department of Computer Science University of Copenhagen

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Function Overloading

CSCE 110 PROGRAMMING FUNDAMENTALS

Doubly-Linked Lists

EECE.3220: Data Structures Spring 2017

Intermediate Programming, Spring 2017*

PIC 10A Objects/Classes

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Instantiation of Template class

III. Classes (Chap. 3)

CSC1322 Object-Oriented Programming Concepts

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

An Introduction to C++

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

CSCE 110 PROGRAMMING FUNDAMENTALS

SFU CMPT Topic: Function Objects

Fundamentals of Type-Dependent Code Reuse in C++ Mark Isaacson

CSCI 104 Exceptions. Mark Redekopp David Kempe

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

class Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // C

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Functions. Ali Malik

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

CSI33 Data Structures

Abstract Data Types (ADT) and C++ Classes

CE221 Programming in C++ Part 1 Introduction

Introduction to Classes

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Advanced C++ Topics. Alexander Warg, 2017

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

C++ Templates. David Camp

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

CSCI 104 Inheritance. Mark Redekopp David Kempe

CS93SI Handout 04 Spring 2006 Apr Review Answers

Operator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lab 2: ADT Design & Implementation

Intermediate Programming & Design (C++) Classes in C++

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

Functions. Ali Malik

Program template-smart-pointers-again.cc

Programming Abstractions

Praktikum: Entwicklung interaktiver eingebetteter Systeme

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

Smart Pointers, deleted functions, and 2-3 trees

Implementing Abstract Data Types (ADT) using Classes

Do not write in this area TOTAL. Maximum possible points: 75

Initializing and Finalizing Objects

CS 103 Unit 11. Linked Lists. Mark Redekopp

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Short Notes of CS201

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Spare Matrix Formats, and The Standard Template Library

Lecture 7. Log into Linux New documents posted to course webpage

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Exercise 6.2 A generic container class

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Abstraction in Software Development

CS201 - Introduction to Programming Glossary By

PIC 10A. Lecture 17: Classes III, overloading

Comp151. Generic Programming: Container Classes

SFU CMPT Topic: Classes

eingebetteter Systeme

C++ (Non for C Programmer) (BT307) 40 Hours

CS 103 Unit 11. Linked Lists. Mark Redekopp

Ch 2 ADTs and C++ Classes

Implementing an ADT with a Class

Transcription:

CSCI 104 Templates Mark Redekopp David Kempe

Overview C++ Templates allow alternate versions of the same code to be generated for various data types 2

FUNCTION TEMPLATES 3

How To's Example reproduced from: http://www.cplusplus.com/ doc/tutorial/templates/ Consider a max() function to return the max of two int's But what about two double's or two strings Define a generic function for any type, T Can then call it for any type, T, or let compiler try to implicitly figure out T int max(int a, int b) if(a > b) return a; else return b; double max(double a, double b) if(a > b) return a; else return b; Non-Templated = Multiple code copies template<typename T> T max(const T& a, const T& b) if(a > b) return a; else return b; int main() int x = max<int>(5, 9); //or x = max(5, 9); // implicit max<int> call double y = max<double>(3.4, 4.7); // y = max(3.4, 4.7); Templated = One copy of code 4

CLASS TEMPLATES 5

Templates We ve built a list to store integers But what if we want a list of double s or string's or other objects We would have to define the same code but with different types What a waste! Enter C++ Templates Allows the one set of code to work for any type the programmer wants The type of data becomes a parameter #ifndef LIST_INT_H #define LIST_INT_H struct IntItem int val; IntItem* next; ; class ListInt public: ListInt(); // Constructor ~ListInt(); // Destructor void push_back(int newval);... private: IntItem* head_; ; #endif #ifndef LIST_DBL_H #define LIST_DBL_H struct DoubleItem double val; DoubleItem* next; ; class ListDouble public: ListDouble(); // Constructor ~ListDouble(); // Destructor void push_back(double newval);... private: DoubleItem* head_; ; #endif 6

Templates Allows the type of variable in a class or function to be a parameter specified by the programmer Compiler will generate separate class/struct code versions for any type desired (i.e instantiated as an object) LList<int> my_int_list causes an int version of the code to be generated by the compiler LList<double> my_dbl_list causes a double version of the code to be generated by the compiler // declaring templatized code template <typename T> struct Item T val; Item<T>* next; ; template <typename T> class LList public: LList(); // Constructor ~LList(); // Destructor void push_back(t newval);... private: Item<T>* head_; ; // Using templatized code // (instantiating templatized objects) int main() LList<int> my_int_list; LList<double> my_dbl_list; my_int_list.push_back(5); my_dbl_list.push_back(5.5125); double x = my_dbl_list.pop_front(); int y = my_int_list.pop_front(); return 0; 7

Templates Writing a template Precede class with: template <typename T> Or template <class T> Use T or other identifier where you want a generic type Precede the definition of each function with template <typename T> In the scope portion of the class member function, add <T> Since Item and LList are now templated, you can never use Item and LList alone You must use Item<T> or LList<T> #ifndef LIST_H #define LIST_H template <typename T> struct Item T val; Item<T>* next; ; template <typename T> class LList public: LList(); // Constructor ~LList(); // Destructor void push_back(t newval); T& at(int loc); private: T* head_; ; template<typename T> LList<T>::LList() head_ = NULL; template<typename T> LList<T>::~LList() template<typename T> void LList<T>::push_back(T newval)... #endif 8

Templates Usually we want you to write the class definition in a separate header file (.h file) and the implementation in a.cpp file Key Fact: Templated classes must have the implementation IN THE HEADER FILE! Corollary: Since we don't compile.h files, you cannot compile a templated class separately Why? Because the compiler would have no idea what type of data to generate code for and thus what code to generate #ifndef LIST_H #define LIST_H template <typename T> struct Item T val; Item<T>* next; ; template <typename T> class LList public: LList(); // Constructor ~LList(); // Destructor void push_back(t newval); private: T* head_; ; #endif #include "List.h" List.h template<typename T> LList<T>::push_back(T newval) if(head_ = NULL) head_ = new Item<T>; // how much memory does an Item // require? List.cpp 9

Templates The compiler will generate code for the type of data in the file where it is instantiated with a certain type #include "List.h" int main() LList<int> my_int_list; LList<double> my_dbl_list; Main.cpp my_int_list.push_back(5); my_dbl_list.push_back(5.5125); double x = my_dbl_list.pop_front(); int y = my_int_list.pop_front(); return 0; // Compiler will generate code for LList<int> when compiling main.cpp #ifndef LIST_H #define LIST_H template <typename T> struct Item T val; Item<T>* next; ; template <typename T> class LList public: LList(); // Constructor ~LList(); // Destructor void push_back(t newval); T& at(int loc); private: T* head_; ; template<typename T> LList<T>::LList() head_ = NULL; template<typename T> LList<T>::~LList() template<typename T> void LList<T>::push_back(T newval)... #endif List.h 10

Exercise Recall that maps/dictionaries store key,value pairs "Billy Bruin" 2.5 Example: Map student names to their GPA "Tommy Trojan" 3.7 "Harry Harvard" 4.3 How many key,value type pairs are there? "Dale Duck" 2.5 string, int int, double Etc. Would be nice to create a generic data structure Define a Pair template with two generic type data members 11

It's an object, it's a function it's both rolled into one! WHAT THE "FUNCTOR" 12

Who you gonna call? Functions are "called" by using parentheses () after the function name and passing some arguments Objects use the. or -> operator to access methods of an object Calling an object doesn't make sense You call functions not objects Or can you? class ObjA public: ObjA(); void action(); ; int main() ObjA a; ObjA *aptr = new ObjA; // This makes sense: a.action(); aptr->action(); // This doesn't make sense a(); // a is already constructed, so // it can't be a constructor call // So is it illegal? return 0; 13

Operator() Calling an object does make sense when you realize that () is an operator that can be overloaded For most operators their number of arguments is implied operator+ takes an LHS and RHS operator-> takes no args You can overload operator() to take any number of arguments of your choosing class ObjA public: ObjA(); void action(); void operator()() cout << "I'm a functor!"; cout << endl; void operator()(int &x) return ++x; ; int main() ObjA a; int y = 5; // This does make sense!! a(); // prints "I'm a functor!" // This also makes sense!! a(y); // y is now 6 return 0; 14

Functors: What are they good for? I'd like to use a certain class as a key in a map or set Maps/sets require the key to have A less-than operator Guess I can't use ObjA Or can I? class ObjA public: ObjA(...); void action(); int getx() return x; string gety() return y; private: int x; string y; ; obja.h Someone else wrote it int main() // I'd like to use ObjA as a key // Can I? map<obja, double> mymap; ObjA(5,"hi") a; mymap[a] = 6.7; return 0; 15

Functors: What are they good for? Map template takes in a third template parameter which is called a "Compare" object It will use this type and assume it has a functor [i.e. operator() ] defined which can take two key types and compare them class ObjA public: ObjA(...); void action(); int getx() return x; string gety() return y; private: int x; string y; ; obja.h Someone else wrote it struct ObjAComparer bool operator()(const ObjA& lhs, const ObjA& rhs) return lhs.getx() < rhs.getx(); ; int main() // Now we can use ObjA as a key!!!! map<obja, double, ObjAComparer> mymap; ObjA(5,"hi") a; mymap[a] = 6.7; return 0; 16

More Uses Functors can act as a user-defined "function" that can be passed as an argument and then called on other data items Below is a modified count_if template function (from STL <algorithm>) that counts how many items in a container meet some condition template <class InputIterator, class Cond> int count_if (InputIterator first, InputIterator last, Cond pred) int ret = 0; for( ; first!= last; ++first) if ( pred( *first ) ) ++ret; return ret; 17

More Uses Functors can act as a user-defined "function" that can be passed as an argument and then called on other data items You need to define your functor struct [with the operator()], declare one and pass it to the function struct NegCond bool operator(int val) return val < 0; ; int main() std::vector<int> myvec; // myvector: -5-4 -3-2 -1 0 1 2 3 4 for (int i=-5; i<5; i++) myvec.push_back(i); NegCond c; int mycnt = count_if (myvec.begin(), myvec.end(), c); cout << "myvec contains " << mycnt; cout << " negative values." << endl; return 0; 18

Final Word Functors are all over the place in C++ and STL Look for them and use them where needed References http://www.cprogramming.com/tutorial/functors-function-objectsin-c++.html http://stackoverflow.com/questions/356950/c-functors-and-theiruses 19

Practice SlowMap wget http://ee.usc.edu/~redekopp/cs104/slowmap.cpp Write a functor so you can use a set of string*'s and ensure that no duplicate strings are put in the set http://bits.usc.edu/websheets/index.php?group=templates strset 20