CSI33 Data Structures

Similar documents
CS197c: Programming in C++

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

September 19,

The Standard Template Library Classes

COMP6771 Advanced C++ Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

CSI33 Data Structures

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSI33 Data Structures

COEN244: Class & function templates

CSC 222: Computer Programming II. Spring 2004

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

G52CPP C++ Programming Lecture 18

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

1. The term STL stands for?

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

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

EL2310 Scientific Programming

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Module 9. Templates & STL

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

Function Overloading

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

COMP6771 Advanced C++ Programming

CSCI 104 Templates. Mark Redekopp David Kempe

CSI33 Data Structures

Lecture on pointers, references, and arrays and vectors

Intermediate Programming, Spring 2017*

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

STL: C++ Standard Library

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Short Notes of CS201

Due Date: See Blackboard

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Cpt S 122 Data Structures. Templates

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

CS201 - Introduction to Programming Glossary By

CSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CS 103 Unit 12 Slides

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

pointers & references

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Linked List using a Sentinel

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

CS193D Handout 12 Winter 2005/2006 January 30, 2006 Introduction to Templates and The STL

CMSC 4023 Chapter 11

CPSC 427a: Object-Oriented Programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Programming. C++ Basics

CMSC 202 Midterm Exam 1 Fall 2015

ADTs: Stacks and Queues

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

CSE 250: Data Structures Fall 2015 Sample Final Exam

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Due Date: See Blackboard

1 Short Answer (7 Points Each)

7 TEMPLATES AND STL. 7.1 Function Templates

Programming Abstractions

Templates and Vectors

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

Abstract Data Types 1

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

Lambda functions. Zoltán Porkoláb: C++11/14 1

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Programming in C/C Lecture 3

Exceptions, Case Study-Exception handling in C++.

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

Bruce Merry. IOI Training Dec 2013

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

CPSC 427: Object-Oriented Programming

Computer Science II Lecture 2 Strings, Vectors and Recursion

Object Oriented Design

Your first C++ program

CSI33 Data Structures

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

ADT: Design & Implementation

Abstract Data Types 1

A <Basic> C++ Course

Programming Abstractions

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

This examination has 11 pages. Check that you have a complete paper.

STL Standard Template Library

Name Section: M/W or T/TH. True or False (14 Points)

Introduction to Programming

Chapter 11. Abstract Data Types and Encapsulation Concepts

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

C++ Exception Handling 1

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Lesson 13 - Vectors Dynamic Data Storage

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College November 22, 2017

Outline Outline 1 Chapter 12: C++ Templates

Outline Chapter 12: C++ Templates 1 Chapter 12: C++ Templates

Templates Allow Code For Different Types Python doesn t associate types with variable names, so the same code might work for different types. In this example, the function Maximum will find the larger of two numbers having the same type (as long as the operator > is defined for that type). For example, the types int, float, and even Rational will work here: def Maximum(a, b): if a > b: return a else: return b

C++: Different Versions For Different Types int maximum int(int a, int b) { if (a > b){ return a; else { return b;

C++: Different Versions For Different Types int maximum double(double a, double b) { if (a > b){ return a; else { return b;

Template Function Example: C++ template <typename Item> Item maximum(item a, Item b) { if (a > b) { return a; else { return b;

Template Function Example: C++ int main() { int a=3, b=4; double x=5.5, y=2.0; cout << maximum(a, b) << endl; cout << maximum(x, y) << endl; return 0;

C++ : Container Classes Container classes which provide certain access to each item (stack, Queue,...) all behave the same for different data types of the items contained. Iterators should be provided to allow abstract traversal (without needing to know how the container is implemented). C++ template classes are able to provide this.

C++ : Container Classes As each container class is used for some datatype, the compiled template class for that type is instantiated. No code for a template class instance is compiled until it is needed.

The Standard Template Library The STL implements most of the common container classes as C++ template classes. It is now a standard part of the C++ library. It defines a wide variety of containers for classes which implement a few basic operations. (For example, < for binary search trees or priority queues.) It provides iterators for these classes.

The vector Template Class int main() { vector<int> iv; vector<double> dv; int i; for (i=0; i<10; ++i) { iv.push back(i); dv.push back(i + 0.5); for (i=0; i< 10; ++i) { cout << iv[i] << " " << dv[i] << endl; return 0;

The vector Template Class #include <iostream> #include <vector> using namespace std; int main() { vector<int> iv(5, 3); vector<double> dv(5); int i; for (i=0; i<5; ++i) { cout << iv[i] << " " << dv[i] << endl;

The vector Template Class #include <iostream> #include <vector> using namespace std; int main() { vector<int> iv; vector<int>::iterator iter; int i; for (i=0; i<10; ++i) { iv.push back(i); for (iter=iv.begin(); iter!= iv.end(); ++iter) { cout << *iter << endl; return 0;

User-Defined The header file, <classname>.h, is the same for ordinary classes, but class definition has a template data type, a wild card typename instead of a normal type like int or double. The class definition is preceded by template <typename T> where T can be any identifier not in use. (for example, Item in the stack class.) Whenever the template data type is needed in a function declaration, it is used like an ordinary type name: bool pop(item &item); The last line of the header file includes the implementation file: #include "<classname>.template" (which does not include the header file).

User-Defined template <typename Item> class Stack { public: Stack(); ~Stack(); int size() const { return size ; bool top(item &item) const; bool push(const Item &item); bool pop(item &item);

User-Defined private: Stack(const Stack &s); void operator=(const Stack &s); void resize(); Item *s ; int size ; int capacity ; ; #include "Stack.template"

User-Defined // Stack.template template <typename Item> Stack<Item>::Stack() { s = NULL; size = 0; capacity = 0; template <typename Item> Stack<Item>::~Stack() { delete [] s ;

User-Defined // test Stack.cpp #include "Stack.h" int main() { Stack<int> int stack; Stack<double> double stack; int stack.push(3); double stack.push(4.5); return 0;