STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.

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.
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.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
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.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Templates and the STL.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structures Using C++ 2E
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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,
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.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 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.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Overview of C++ Templates
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 The Standard Template Library Drozdek Section 3.7.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
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.
Motivation for Generic Programming in C++
Programming with ANSI C ++
Standard Template Library
C++ Templates.
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
C++ Programming Standard Library
Iterators and STL Containers
Standard Template Library
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley Longman, Inc. ● ISBN: ● My presentation will come from both books plus a small amount from other references. –

History ● 1989 – Start of standardization of C++ ● 1997 – Finished, published Sept ● 750 pages, published by the International Standards Organization (ISO). ● Title: “Information Technology – Programming Languages – C++” ● “..it defines the exact contents and behavior of C++,...” ● Part of the standard is a standard library.

History: C++ Standard Library ● core components for – I/O – strings, – containers (data structures) – algorithms (such as sort, search and merge) – support for numeric computation – support for internationalization (such as different character sets)

History: STL added ● 1994 – The STL was added to the C++ standard library. ● STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. ● Makes heavy use of both function and class templates.

Templates ● Templates are functions or classes that are written for one or more types not yet specified. ● When you use a template, you pass the types as arguments, explicitly or implicitly.

Template Example ● template ● inline const T& my_max (const T& a, const T& b) ● { ● if(a < b) return b; ● else return a; ● }

exmaple : details ● First line defines T as an arbitrary data type that is specified by the caller when the caller calls the function. The type is classified by class, although it does not have to be a class. You can use any data type as long as it provides the operations that the template uses. ● inline is a hint to the compiler to put this function in-line since it is so short.

example: details ● const before the type reference allows the function to use constant reference parameters. ● T& is the type of the function. (A reference to the type T) ● my_max is the name of the function being defined as a template. ● const T& a (and const T& b) define two reference parameters whose value cannot be changed.

example: details ● The code for the function itself is simply to return the maximum of the two parameter values as the value of the function. ● Instantiated by its use (since it is a function and not a class). ● Use: – int a(10), b(12); – cout << my_max(a,b) << endl; ● Output: 12

MyArray class template – template – class MyArray { – public: – MyArray(int = 10); – ~MyArray( ); – void FillArray(T=2); – void PrintArray( ); – private: – T* ArrayPtr; – int size; – };

MyArray constructor – template – MyArray ::MyArray(int s) – { – if(s < 0) s = 10; – size = s; – ArrayPtr = new T[s]; – }

MyArray destructor – template – MyArray ::~MyArray( ) – { – delete [ ] ArrayPtr; – }

MyArray FillArray function – template – void MyArray ::FillArray(T value) – { – for(int i=0; i<size; i++) – ArrayPtr[i]=value; – }

MyArray PrintArray function – template – void MyArray ::PrintArray( ) – { – for(int i = 0; i<size; i++) – cout << ArrayPtr[i] << “ “; – cout << endl; – }

test MyArray – #include “MyArray.h” – int main (void) – { – MyArray fred(3); – MyArray suzie(5); – MyArray sam; – fred.FillArray(-2); fred.PrintArray(); – suzie.FillArray(3.25); suzie.PrintArray(); – sam.FillArray(); sam.PrintArray(); – }

test output – – –

STL General Concepts ● Namespace std ● (covered before) – std::cout << std::hex << 127 << std::endl; – or – using std::cout; – using std::endl; – cout << std::hex << 127 << endl; – or – using namespace std; – cout << hex << 127 << endl;

STL General Concepts cont. ● Header files ● In addition to the standard ones: – #include ● There is normally a special include for each use of a STL template. – #include ● (more specifics as we describe STL components)

STL Components ● Containers – Manage collections of objects of a certain kind. ● Iterators – Used to step through the elements of collections of objects. – Small but common interface for an arbitrary container type. ● Algorithms – Used to process elements of collections

container algorithm iterator

STL Sequence Containers ● Ordered Collections. ● Every element has a certain position. ● Position independent of the value of the element. ● Position depends on order and place of insertion. – Vector – Deque – List

STL Sequence Containers Vector Deque List

STL Associative Containers ● Sorted Collection. ● Actual position of an element depends on its value due to certain sorting criterion. ● Order of insertion doesn't matter. – Set – Multiset – Map – Multimap

STL Associative Containers Set/Multiset Map/Multimap

STL Iterators – An object that can “iterate” over elements. May be all or part of a STL container. – An iterator represents a certain position in a container. – Operator * ● Returns the element of the actual position. – Operator ++ ● Lets the iterator step forward to the next element. ● Most iterators also allow stepping backwards by using operator - -

(cont.) ● Operator == and != ● Returns whether two iterators represent the same position. – Operator = ● Assigns an iterator (the position of the element to which it refers)

Iterator related member functions ● begin( ) – returns an interator that represents the beginning of the elements in the container. ● end( ) – returns an iterator that represents the end of the elements in the container. – The end position is behind the last element.

begin ( ) end ( )