Copyright © 2009 – Curt Hill Standard Template Library An Introduction.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

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.
Data Structures Using C++ 2E
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
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)
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Abstract Data Types and Encapsulation Concepts
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Data Structures Using C++ 2E
OOP Languages: Java vs C++
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
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.
Data Structures Using C++ 2E
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
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.
1 Iterators Good reference site:
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
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.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2009 Curt Hill Look Ups A Recurring Theme.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
11.1 The Concept of Abstraction
A Sorted, Unique Key Container
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Standard Template Library
Standard Template Library
11.1 The Concept of Abstraction
A dictionary lookup mechanism
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Copyright © 2009 – Curt Hill Standard Template Library An Introduction

Generics The template feature of C++ allows the separation of logic from data type –Sorting an array is always the same –The only thing that changes is type and location of the key The STL implements a number of well known abstract data types as groups of template classes Originally developed at HP Copyright © 2009 – Curt Hill

Components of the STL Containers –Our main focus Algorithms Iterators Function templates Adapters Copyright © 2009 – Curt Hill

Containers Containers are typically one or more template classes that store other items The items they store may be classes or primitives Each class has its own requirements for the characteristics of the stored class or primitive –May be as little as suitable for assignment Every object can have a pointer reference it, which is suitable for assignment but not comparison –May be as complicated suitable for equality or less comparison Copyright © 2009 – Curt Hill

Example Containers Vector List Deque (a form of queue) Sets and bags (multisets) Maps and multimaps (trees or dictionaries) We will consider all of these Copyright © 2009 – Curt Hill

Algorithms Common algorithms for manipulating a container –Sort –Search –Comparison These algorithms require that the contained class be suitable for comparison –This will often obligate us to overload comparison operators Copyright © 2009 – Curt Hill

Iterators The mechanism to traverse an entire container Also used to input/output data to/from the container Iterators are generally classes made to look like pointers Often get one or need one for most data access Copyright © 2009 – Curt Hill

Function templates Many algorithms accept a function template as a parameter that modifies how the algorithm works Usually there is some place in the algorithm where an operation is needed This allows the user to write a template function that handles it These functions may do arithmetic or comparison operation when we have not overloaded other operators Copyright © 2009 – Curt Hill

Adapters An adaptor modifies the interface of other components This allows us to glue components together to get larger more usable items Container adapter –Transforms a container to function with specified characteristics –There is no stack container but a stack adapter that can make a list into a stack Iterator adapters give more options than our regular iterators Function adaptors change the characteristics of functions Copyright © 2009 – Curt Hill

A problem The STL causes some issues Complicated actions are being done to your objects This is in an internal environment you know next to nothing about Since you do not know what it is doing, it is a good idea to code classes following some recommendations Copyright © 2009 – Curt Hill

Class Recommendations Code a default constructor –Either make it private or code it for real –This prevents the compiler from generating a phony one –If it is private then nobody can use it without an error Code a copy constructor –This will be used by the STL –Makes sure that it works properly Copyright © 2009 – Curt Hill

More Recommendations Code a destructor –It should deallocate pointers (carefully) –It should close files Overload the assignment operator –This will often just invoke the copy constructor –Make it private if something is not copyable –If not copyable it should not be in an STL container class Copyright © 2009 – Curt Hill

Comparison operators Overload equality and less than They will usually require code These are the most likely to be used by STL All the rest can be constructed from those two If no comparison is possible then make a body with an assert in it Keeps from accidental use by member code Make private – prevents accidental use by non-member code Copyright © 2009 – Curt Hill

Recommendation Summary Every primitive and most good components have these already These are also good recommendations for any classes containing files or pointers Consider the ClassMaker program on the downloads page Copyright © 2009 – Curt Hill