CS212: Object Oriented Analysis and Design

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.
Linear Lists – Array Representation
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures Using C++ 2E
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
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)
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
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
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++ 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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
1 Iterators Good reference site:
 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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
A gentle introduction to the standard template library (STL) Some references:
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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.
Object-Oriented Programming (OOP) Lecture No. 41
Motivation for Generic Programming in C++
Standard Template Library
C++ Programming:. Program Design Including
Programming with ANSI C ++
Standard Template Library
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Vectors Holds a set of elements, like an array
Standard Template Library
Standard Template Library (STL)
Chapter 4 Linked Lists.
STL Common tools for C++.
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Array Lists Chapter 6 Section 6.1 to 6.3
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Doubly Linked List Implementation
The Standard Template Library
Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Collections Intro What is the STL? Templates, collections, & iterators
Doubly Linked List Implementation
An Introduction to STL.
The List Container and Iterators
SPL – PS1 Introduction to C++.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

CS212: Object Oriented Analysis and Design Standard Template Library

Introduction Templates facilitates generic programming STL (Standard Template Library) is a powerful set of C++ template classes Provides general-purpose templatized classes and functions Implements many popular and commonly used algorithms and data structures

STL Components STL Iterator Algorithm Container

Containers

Algorithms Algorithms act on containers Provide the means by which contents of containers can be modified Initialization, sorting, searching, and transforming the contents of containers Many algorithms operate on a range of elements within a container.

Iterators Iterators are objects that are, more or less, pointers Ability to cycle through the contents of a container Iterator Access Allowed Random Access Store and retrieve values. Elements may be accessed randomly. Bidirectional Store and retrieve values. Forward and backward moving. Forward Store and retrieve values. Forward moving only. Input Retrieve, but not store values. Forward moving only. Output Store, but not retrieve values. Forward moving only.

Other STL Elements Allocators : manage memory allocation for a container Predicates : returns true/ false Comparison functions Function objects

General Theory of Operation Decide on the type of container to use Use its member functions to add elements to the container, access or modify those elements, and delete elements Access the elements within a container is through an iterator

Allocator Encapsulates a memory allocation and deallocation strategy Used by every standard library component All standard library containers and other allocator-aware classes access the allocator Demonstration

Vectors The most general-purpose of the containers Supports a dynamic array Standard array subscript notation to access its elements template <class T, class Allocator = allocator<T>> class vector

Vector: Constructors Constructs an empty vector explicit vector(const Allocator &a = Allocator( ) ); explicit vector(size_type num, const T &val = T ( ), const Allocator &a = Allocator( )); vector(const vector<T, Allocator> &ob); template <class InIter> vector(InIter start, InIter end, Constructs a vector that has num elements with the value val Constructs a vector that contains the same elements as ob Constructs a vector that contains the elements in the range specified by the iterators start and end

Constraints Any object that will be stored in a vector must define a default constructor It must also define the < and == operations All of the built-in types automatically satisfy these requirements. Implementation is compiler dependent

Instantiating vectors // create zero-length int vector vector<int> iv; vector<char> cv(5); vector<char> cv(5, 'x'); vector<int> iv2(iv); // create 5-element char vector // initialize a 5-element char vector // create int vector from an int vector

Common functions Member Description size() Returns the current size of the vector begin() Returns an iterator to the start of the vector end() Returns an iterator to the end of the vector push_back() Puts a value onto the end of the vector insert() Add elements to the middle erase() Remove elements from a vector

Using Iterators Pointer like objects in STL STL algorithms uses them to traverse through the container An array can be accessed either through subscripting or through a pointer The members of a vector using subscripting or through the use of an iterator Demonstration

Insert and Delete Insert element at a given location Delete element from a given location Demonstration

Storing Class Objects Vectors are not limited for built-in types Can store any type of objects (user defined types) It must also define the < and == operations Demonstration

Plug compatible

Major categories of STL

Hierarchical relationship between STL iterator categories

Accumulate

Input Iterator Reads from a input sequence (built-in type, user-defined type, stream) It refers to a family of types ++, *, == operator to be defined for the type on which to iterate

Output iterators Allow us to write values to a sequence Do not guarantee that we can read from the sequence ==, != need not be defined for the output iterator

Forward iterators Input operator writes value to a sequence, output iterator reads from a sequence Forward iterator allows both reading, writing and traverse in one direction It is possible to save a forward iterator Later start from the same position (multipass algorithm)

Forward iterator One example where forward iterator is used, STL replace

Bidirectional Iterator Forward iterators allow traverse in a single direction Bidirectional iterator allows traversal in either direction Both prefix and postfix version of operator-- is required STL reverse algorithm can be used

Random access iterator To support algorithms with greater constraints Any position in a sequence be reachable from any other in constant time Similar to bidirectional iterator, plus Addition and subtraction of an integer Use of Offset Bi-directional “Big-jumps” Iterator subtraction Comparison operator >, >=, < , <=

STL Iterator Hierarchy Why it is useful to classify iterators into categories Classification is an iterator hierarchy Iterator categories are used in the specification of the container and the algorithm e.g. List provides bidirectional iterators, and find requires input iterator. So, find can be used with lists Input, Output Forward Bidirectional Random Access What about sort??

Insert Iterator Insert iterators are special output iterators Prevents overwrite at a particular location Insert new elements at a specific position in the container The container needs to have an insert member function