Templates and the STL.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Data Structures Using C++ 2E
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Introduction to the STL
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Containers: Vectors
More on the STL vector list stack queue priority_queue.
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.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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)
C++ STL CSCI 3110.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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,
1 Read § Templates Templates allow functions and classes to be _________________ so that the Templates provide a means to ________________—
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.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
 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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
The Standard Template Library Container Classes Version 1.0.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
C++ Review STL 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.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
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.
Object-Oriented Programming (OOP) Lecture No. 41
Regarding homework 9 Many low grades
C++ Templates.
Standard Template Library (STL)
What remains Topics Assignments Final exam
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Templates and Standard Containers
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Iterators and STL Containers
Standard Template Library
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

Templates and the STL

Template (generic) classes the behavior of a container class does not depend of the kind of elements stored in the container how can we create a generic (type independent) collection class using C++? drawbacks of typedef have to change source code (recompile) for each type can’t have multiple containers whose elements are of different types an alternative: define a class template template for creating a class

Function templates template - a pattern or a mold functions/methods have data parameters void f (int p1, float p2, Customer p3); argument provided when function is called function templates also have type parameters from one function template multiple actual functions can be generated (by the compiler) allow a programmer to write "generic" functions - type independent

overloaded Swap differ only in the type! void Swap(int & first, int & second) { int temp = first; first = second; second = temp; } differ only in the type! void Swap(string & first, string & second) { string temp = first; first = second; second = temp; }

generic Swap template <typename Item> void Swap(Item & first, Item & second) { Item temp = first; first = second; second = temp; } 1. Swap is a function template 2. Item is a type parameter 3. keyword typename and class are interchangeable 4. template function prototype and definition must be in the same file 5. actual type substituted for Item must be "assignable"

Function instantiation no object code is created from a function template compiler generates object code for an actual function from a function template when needed when compiling a call to the function substitutes argument type for the type parameter each call with a different argument type results in object code for a new actual function Swap(i, j); // i and j are ints - Item replaced by int Swap(s1, s2); // s1 and s2 are strings - Item replaced by string etc.

Class templates make it possible to have several objects of a container class, each of which holds a different type of element Stack of ints; Stack of strings; Stack of ? declaration and implementation of a template class have to be compiled together template class implementation requires "messy" syntax

Class template for Stack template < typename SE > class Stack { public: - - - Stack ( ); void push (const SE & newElement); SE top ( ) const; private: SE myArray[STACK_CAPACITY]; int myTop; }; template directive Class name is Stack<SE>

declaring Stack<SE> objects int main ( ) { Stack <int> samples; Stack <float> cost; Stack <string> names; Stack <customer> line; - - -

implementing a template class cumbersome syntax needed each method is a template so is preceded by the template directive class name is Stack<SE> template <typename SE> Stack<SE>::Stack( ) { - - - - }

Compiling template classes compiler cannot separately compile a template class compiler needs to know the actual type to substitute compiler generates code when a template class object is declared substitutes actual type for the type parameter needs access to the class implementation, not just the class declaration compiler generates separate code for each actual type substituted for the type parameter

Two solutions Client.cpp Client.cpp stack.h stack #include “stack” #include “stack.h” client program client program stack.h stack Stack template class declaration Stack template class declaration followed by implementation #include “stack.cpp” stack.cpp implementation

Standard Template Library (STL) is a library of generic container classes which are both efficient and functional C++ STL developed in early 90's at Hewlett Packard Laboratories Alex Stepanov and Meng Lee became part of C++ standard in 1994 implementations available by late 90's see web page for STL Programmer's Guide

STL and Reuseability STL is part of a movement toward libraries of reusable code function libraries (reusable algorithms) class libraries (reusable ADTs) Java 1.2 introduced a library of Collection classes http://java.sun.com/docs/books/tutorial/ index.html data and algorithms packaged together (O-O) STL separates data and algorithms iterators allow algorithms to operate on data

STL components Containers Algorithms Iterators Container classes templates for classes which hold a collection of elements Algorithms templates for functions which operate on a range of elements from a container range is specified by iterators Iterators give access to the elements in a container allow for movement from one element to another Container classes Algorithms Iterators

STL container classes Sequences - elements arranged in a linear order vector<T> - fast inserts only at the end; random access list<T> - fast inserts anywhere; no random access deque<T> - fast inserts at the beginning and the end Adapters - provides a new interface (set of operations) for one of the sequences stack<T> ex: stack<int> operands; queue<T> ex: queue<Customer> line; priority_queue<T> Associative Containers - elements have key values set, map, multiset, multimap

vector<T> growable, self-contained, type-independent array element type specified when a vector is declared vector<double> numbers; vector<cashier> checkOutStations; has a set of operations (methods) capacity increases when needed some vectors vector<int> v1; (capacity is 0) vector<float> v2 (10); (capacity is 10; size is 10) vector<string> v3 (5, "C++"); (capacity is 5; size is 5)

Some vector<T> methods V.capacity( ) //size of array currently allocated V.size( ) //number of values V contains V.empty( ) //true iff V.size( ) is 0 V.reserve(n) //grow V so its capacity is n V.push_back(val) //add val at end of V V.pop_back( ) //erase V's last element V[i] //access element of V whose index is i V.at(i) //access element of V whose index is i

#include <iostream> #include <vector> using namespace std; bool Search(const vector<int> & V, int item); int main ( ) { vector<int> numbers; int number; while (cin >> number) { // enter <control> D to stop the loop if (Search(numbers, number)) cout << "Duplicate" << endl; else numbers.push_back(number); } cout << "number of unique values: " << numbers.size( ); return 0; bool Search(const vector<int> & V, int item) { int p = 0; while(p < V.size( ) ) if (item = = V[p]) // or V.at(p) return true; else p++; return false;

STL iterators iterators are "pointer-like" objects provide a generic way to access the elements of any container class many STL algorithms require iterators as arguments some STL algorithms return an iterator each STL container class has an iterator class associated with it vector<T>::iterator list<T>::iterator stack<T> and queue<T> don't have iterators why?

Iterator categories category determines available operations forward iterator iter++ (increment) *iter (dereference) == and != (equality comparison) bidirectional iterator adds iter-- (decrement) random-access iterator adds iter[n] (constant time access to arbitrary element) iter =+ n (increment n times)

STL Algorithms are function templates designed to operate on a sequence of elements rather than methods the sequence is designated by two iterators most container classes have the following two methods begin( ) - returns an iterator positioned at the container's first element end( ) - returns an iterator positioned past the container's last element (past-the-end) C.begin( ), C.end( ) specifies a sequence which contains all elements of the container C

STL Algorithms some examples of STL algorithms find (iter1, iter2, value) //returns an iterator max_element (iter1, iter2) //returns an iterator sort (iter1, iter2) //sorts using < for_each (iter1, iter2, F) //applies F to every //item see STL Programmer's Guide link on home page

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main ( ) { vector<int> numbers; int number; while (cin >> number) if (find (numbers.begin ( ), numbers.end ( ), number) != numbers.end ( )) cout << "Duplicate" << endl; else numbers.push_back(number); } cout << "number of unique values: " << numbers.size( ); return 0;

Press any key to continue #include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; void set (int & val); void display (int val); int main( ) { vector<int> A(5); for_each (A.begin ( ), A.end ( ), set); // would not work if vector<int> A; used for_each (A.begin ( ), A.end ( ), display); cout << endl; sort (A.begin ( ), A.end ( )); // operator< must be defined for A's element type return 0; } void set (int & val) { val = rand ( ); void display (int val) { cout << " " << val; 41 18467 6334 26500 19169 41 6334 18467 19169 26500 Press any key to continue

list<T> class another STL container class used for storing a linear collection of like items comparison to a vector? linked list vs array is the underlying data structure no indexing (iterators are bidirectional) inserts and deletes anywhere are done in a constant amount of time

a list<T> data structure head size ------ 2

Basic list class methods list( ); // construct an empty list list (const list<T> & aList); // copy constructor ~list( ); // destructor list<T> operator= (const list<T> & aList); // assignment operator bool empty( ); int size( );

Some more list methods L.push_back(value) // append value to L L.push_front(value) // insert value at front of L L.insert(pos, value) // insert value into L at // position indicated by iterator pos L.front( ) // return L's first element L.back( ) // return L's last element L.begin( ) // return an iterator positioned at start L.end( ) // return the"past the end" iterator L.sort( ) // sort L's elements using < Why not sort (L.begin( ), L.end( )); ?

#include <list> #include <iostream> using namespace std; int main ( ) { list<int>L; L.push_back (9); L.push_back (7); L.push_back (5); L.push_back (3); list<int>::iterator p; for (p = L.begin ( ); p != L.end ( ); p++) cout << *p << endl; (*p)++; return 0; } 9 7 5 3 10 8 6 4 Press any key to continue