C++ How to Program, 8/e ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

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.
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.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
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,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Containers: Vectors
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Basic C++ Sequential Container Features
Templates and the STL.
Writing Your Own STL Container Ray Lischner
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:
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Data Structures Using C++ 2E
Containers Overview and Class Vector
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
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.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 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.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Intro to the C++ STL Timmie Smith September 6, 2001.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
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-383 Object-Oriented Programming & Design Lecture 30.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Object-Oriented Programming (OOP) Lecture No. 42.
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
Programming with ANSI C ++
Standard Template Library
C++ Templates.
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CS212: Object Oriented Analysis and Design
priority_queue<T>
Standard Template Library Model
Iterators and STL Containers
Standard Template Library
Presentation transcript:

C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.

 The Standard Template Library (STL) defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. © by Pearson Education, Inc. All Rights Reserved. algorithms + data structures = programs

 Three key components— ◦ containers (popular templatized data structures), ◦ iterators ◦ algorithms  The STL containers are data structures capable of storing objects of almost any data type (there are some restrictions).  We’ll see that there are three styles of container classes ◦ first-class containerssequence / associative ◦ container adaptersstack, queue, priority_queue ◦ near containersbitset, valarray, C-style array © by Pearson Education, Inc. All Rights Reserved.

 Each STL container has associated member functions.  A subset of these member functions is defined in all STL containers. © by Pearson Education, Inc. All Rights Reserved.

 STL iterators, which have properties similar to those of pointers, are used by programs and STL algorithms to manipulate the STL-container elements.  Standard arrays can be manipulated by STL algorithms, using standard pointers as iterators.  There are five categories of iterators ◦ input ◦ output ◦ forward ◦ bidirectional ◦ random access © by Pearson Education, Inc. All Rights Reserved.

 Each algorithm has minimum requirements for the types of iterators that can be used with it.  We’ll see that each first-class container supports specific iterator types, some more powerful than others.  A container’s supported iterator type determines whether the container can be used with a specific algorithm  1 st class containers ◦ sequencevector, deque, list ◦ associativeset, multiset, map, multimap © by Pearson Education, Inc. All Rights Reserved.

 Iterators encapsulate the mechanism used to access container elements.  This encapsulation enables many of the STL algorithms to be applied to various containers without regard for the underlying container implementation.  As long as a container’s iterators support the minimum requirements of the algorithm, the algorithm can process that container’s elements.  This also enables you to create new algorithms that can process the elements of multiple container types. © by Pearson Education, Inc. All Rights Reserved.

 The containers are divided into three major categories ◦ sequence containers ◦ associative containers ◦ container adapters © by Pearson Education, Inc. All Rights Reserved.

 The sequence containers represent linear data structures, such as vectors and linked lists.  Associative containers are nonlinear containers that typically can locate elements stored in the containers quickly.  Such containers can store sets of values or key/value pairs.  The sequence containers and associative containers are collectively referred to as the first-class containers  stacks and queues actually are constrained versions of sequential containers (container adapters)  Container adapters do not use iterators! © by Pearson Education, Inc. All Rights Reserved.

 "near containers" ◦ C-like pointer-based arrays ◦ bitset s for maintaining sets of flag values and ◦ val-array s for performing high-speed mathematical vector operations  exhibit capabilities similar to those of the first-class containers, but do not support all the first-class-container capabilities.  Type string supports the same functionality as a sequence container, but stores only character data. © by Pearson Education, Inc. All Rights Reserved.

 Iterators have many similarities to pointers and are used to point to first-class container elements.  Iterators hold state information sensitive to the particular containers on which they operate; thus, iterators are implemented appropriately for each type of container.  Certain iterator operations are uniform across containers.  For example, the dereferencing operator ( * ) dereferences an iterator so that you can use the element to which it points.  The ++ operation on an iterator moves it to the container’s next element (much as incrementing a pointer into an array aims the pointer at the next array element. © by Pearson Education, Inc. All Rights Reserved.

 STL first-class containers provide member functions begin and end.  Function begin returns an iterator pointing to the first element of the container.  Function end returns an iterator pointing to the first element past the end of the container (an element that doesn’t exist). © by Pearson Education, Inc. All Rights Reserved.

 An object of type iterator refers to a container element that can be modified.  An object of type const_iterator refers to a container element that cannot be modified. © by Pearson Education, Inc. All Rights Reserved.

 We use iterators with sequences (also called ranges)  These sequences can be in containers, or they can be input sequences or output sequences. © by Pearson Education, Inc. All Rights Reserved.

 The iterator category that each container supports determines whether that container can be used with specific algorithms in the STL.  Containers that support random-access iterators can be used with all algorithms in the STL.  As we’ll see, pointers into arrays can be used in place of iterators in most STL algorithms, including those that require random-access iterators. © by Pearson Education, Inc. All Rights Reserved.