C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)

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

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.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Mutating Algorithms All the algorithms we have seen have not modified the containers Some algorithms do modify values They do not modify iterators, only.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
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,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Data Structures Using C++ 2E
Templates and the STL.
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
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Intro to the C++ Std Library.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Data Structures Using C++1 Chapter 4 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.
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.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Introduction to the ISO C++ Standard Template Library (STL)
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Intro to the C++ STL Timmie Smith September 6, 2001.
The Standard Template Library Container Classes Version 1.0.
Standard Template Library (STL) - Use Vector and Deque
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Introduction to Templates and Standard Template Library 1.
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.
CS212: Object Oriented Analysis and Design
C++ Programming:. Program Design Including
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Chapter 22: Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Containers, Iterators, Algorithms, Thrust
Elements are always copied when they are put into a container
Containers and the Standard Template Library (STL)
Standard Version of Starting Out with C++, 4th Edition
Standard Template Library
Standard Template Library
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)

C++ Programming: Program Design Including Data Structures, Second Edition2 Objectives In this chapter you will: Learn about the Standard Template Library (STL) Become familiar with the basic components of the STL: containers, iterators, and algorithms Explore how various containers are used to manipulate data in a program Discover the use of iterators Learn about various generic algorithms

C++ Programming: Program Design Including Data Structures, Second Edition3 Introduction ANSI/ISO Standard C++ is equipped with a Standard Template Library (STL) The STL provides class templates to process lists, stacks, and queues This chapter discusses many important features of the STL and shows how to use its tools

C++ Programming: Program Design Including Data Structures, Second Edition4 Components of the STL Components of the STL: −Containers −Iterators −Algorithms Containers and iterators are class templates Iterators are used to step through the elements of a container Algorithms are used to manipulate data

C++ Programming: Program Design Including Data Structures, Second Edition5 Container Types Containers are used to manage objects of a given type Three categories: −Sequence (sequential) containers −Associative containers −Container adapters

C++ Programming: Program Design Including Data Structures, Second Edition6 Sequence Containers Every object has a specific position Three predefined sequence containers: −Vector −Deque −List

C++ Programming: Program Design Including Data Structures, Second Edition7 Sequence Container: Vector A vector container stores and manages its objects in a dynamic array To use a vector container in a program, the program must #include The class vector contains several constructors, including the default constructor

C++ Programming: Program Design Including Data Structures, Second Edition9 Sequence Container: vector (continued) Basic vector operations −Item insertion −Item deletion −Stepping through the elements

C++ Programming: Program Design Including Data Structures, Second Edition10 Sequence Container: vector (continued) vector elements can be accessed using the operations below:

C++ Programming: Program Design Including Data Structures, Second Edition11 Declaring an Iterator to a Vector Container vector contains a typedef iterator For example, the statement vector ::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int

C++ Programming: Program Design Including Data Structures, Second Edition12 Container and Functions begin and end Every container contains the member function begin and end −begin returns the position of the first element −end returns the position of the last element Both functions have no parameters

C++ Programming: Program Design Including Data Structures, Second Edition13 Operations of the vector Class Some vector member functions:

C++ Programming: Program Design Including Data Structures, Second Edition17 The copy Algorithm Function copy: convenient way to output the elements of a container Can be used with any container type Allows you to copy the elements from one place to another −Can output the elements of a vector −Can copy the elements of one vector into another

C++ Programming: Program Design Including Data Structures, Second Edition18 The ostream Iterator and the Function copy One way to output the contents of a container is to use a for loop, along with begin (initialize) and end (loop limit) copy can output a container; an iterator of the type ostream specifies the destination When you create an iterator of the type ostream, specify the type of element that the iterator will output

C++ Programming: Program Design Including Data Structures, Second Edition19 Sequence Container: deque deque stands for double ended queue Implemented as dynamic arrays Elements can be inserted at both ends A deque can expand in either direction Elements are also inserted in the middle

C++ Programming: Program Design Including Data Structures, Second Edition22 Sequence Container: list Lists are implemented as doubly linked lists Every element in a list points to both its immediate predecessor and its immediate successor (except the first and last element) The list is not a random access data structure

C++ Programming: Program Design Including Data Structures, Second Edition26 Iterators An iterator points to the elements of a container (sequence or associative) Iterators provide access to each element The most common operations on iterators are ++ (increment), and * (dereference)

C++ Programming: Program Design Including Data Structures, Second Edition27 Types of Iterators Five types of iterators: −Input iterators −Output iterators −Forward iterators −Bidirectional iterators −Random access iterators

C++ Programming: Program Design Including Data Structures, Second Edition28 Input Iterators Input iterators, with read access, step forward element-by-element  return the values element-by-element Input iterators are provided for reading data from an input stream

C++ Programming: Program Design Including Data Structures, Second Edition30 Output Iterators Output iterators, with write access, step forward element-by-element Output iterators are provided for writing data to an output stream

C++ Programming: Program Design Including Data Structures, Second Edition32 Forward Iterators Forward iterators combine all of the functionality of input iterators and almost all of the functionality of output iterators

C++ Programming: Program Design Including Data Structures, Second Edition34 Bidirectional Iterators Bidirectional iterators are forward iterators that can also iterate backward over the elements The operations defined for forward iterators apply to bidirectional iterators Use the decrement operator to step backward

C++ Programming: Program Design Including Data Structures, Second Edition35 Random Access Iterators Random access iterators are bidirectional iterators that can randomly process the elements of a container Can be used with containers of the types vector, deque, string, as well as arrays Operations defined for bidirectional iterators apply to random access iterators

C++ Programming: Program Design Including Data Structures, Second Edition37 typedef iterator Every container contains a typedef iterator The statement vector ::iterator intVecIter; declares intVecIter to be an iterator into a vector container of the type int

C++ Programming: Program Design Including Data Structures, Second Edition38 typedef const_iterator With the help of an iterator into a container and the dereference operator, *, you can modify the elements of the container If the container is declared const, then we must prevent the iterator from modifying the elements Every container contains typedef const_iterator to handle these situations

C++ Programming: Program Design Including Data Structures, Second Edition39 Stream Iterators istream_iterator −Used to input data into a program from an input stream ostream_iterator −Used to output data from a program into an output stream

C++ Programming: Program Design Including Data Structures, Second Edition40 Associative Containers Elements in associative container are automatically sorted according to some ordering criteria The predefined associative containers in the STL are: −Sets −Multisets −Maps −Multimaps

C++ Programming: Program Design Including Data Structures, Second Edition41 Associative Containers: set and multiset Associative containers set and multiset automatically sort their elements multiset allows duplicates, set does not The default sorting criterion is the relational operator <(less than); that is, the elements are arranged in ascending order

C++ Programming: Program Design Including Data Structures, Second Edition44 Container Adapters The STL provides containers to accommodate special situations called container adapters The three container adapters are: −Stacks −Queues −Priority Queues Container adapters do not support any type of iterator

C++ Programming: Program Design Including Data Structures, Second Edition45 Stack The STL provides a stack class

C++ Programming: Program Design Including Data Structures, Second Edition46 Queue The STL provides a queue class

C++ Programming: Program Design Including Data Structures, Second Edition48 Algorithms Operations such as find, sort, and merge are common to all containers and are provided as generic algorithms STL algorithms can be classified as follows −Nonmodifying algorithms −Modifying algorithms −Numeric algorithms −Heap algorithms

C++ Programming: Program Design Including Data Structures, Second Edition49 Nonmodifying Algorithms Nonmodifying algorithms do not modify the elements of the container

C++ Programming: Program Design Including Data Structures, Second Edition50 Modifying Algorithms

C++ Programming: Program Design Including Data Structures, Second Edition51 Numeric Algorithms Numeric algorithms perform numeric calculations on the elements of a container Numeric algorithms: −accumulate −inner_product −adjacent_difference −partial_sum

C++ Programming: Program Design Including Data Structures, Second Edition52 Heap Algorithms Heap sort algorithm sorts array data The array containing the data is viewed as a binary tree Heap algorithms: −make_heap −push_heap −pop_heap −sort_heap

C++ Programming: Program Design Including Data Structures, Second Edition53 Function Objects A function object contains a function that can be treated as a function using the function call operator, () A function object is a class template that overloads the function call operator, () Predicates are special types of function objects that return Boolean values Predicates are typically used to specify searching or sorting criteria

C++ Programming: Program Design Including Data Structures, Second Edition55 Insert Iterators The STL provides three iterators, called insert iterators, to insert the elements at the destination: −back_inserter −front_inserter −inserter

C++ Programming: Program Design Including Data Structures, Second Edition56 STL Algorithms STL algorithms include documentation with the function prototypes The parameter types indicate for which type of container the algorithm is applicable fill: fills a container with elements fill_n: fills in the next n elements; the element that is used as a filling element is passed as a parameter

C++ Programming: Program Design Including Data Structures, Second Edition57 STL Algorithms (continued) generate and generate_n: generate elements and fill a sequence find, find_if, find_end, and find_first_of: find elements in a given range remove: removes certain elements from a sequence remove_if: removes elements from a sequence by using some criteria

C++ Programming: Program Design Including Data Structures, Second Edition58 STL Algorithms (continued) remove_copy, remove_copy_if : copies the elements of a sequence into another sequence by excluding certain elements of the first sequence swap, iter_swap, and swap_ranges: swap elements search, search_n, sort, and binary_search: search and sort elements, and described in the header file algorithm

C++ Programming: Program Design Including Data Structures, Second Edition59 STL Algorithms (continued) Function replace is used to replace all occurrences, within a given range, of a given element with a new value Function replace_if is used to replace the values of the elements, within a given range, satisfying certain criteria with a new value The function replace_copy is a combination of replace and copy The function replace_copy_if is a combination of replace_if and copy

C++ Programming: Program Design Including Data Structures, Second Edition60 STL Algorithms (continued) adjacent_find: finds the first occurrence of consecutive elements that meet criteria merge: merges the sorted lists. Both lists must be sorted according to the same criteria, for example, both must be in ascending order inplace_merge: combines sorted sequences

C++ Programming: Program Design Including Data Structures, Second Edition61 STL Algorithms (continued) reverse: reverses the order of the elements in a given range reverse_copy: reverses the elements of a given range while copying into a destination range; the source is not modified rotate: rotates the elements of a given range rotate_copy: combination of rotate and copy. Elements of the source are copied at the destination in a rotated order; the source is not modified

C++ Programming: Program Design Including Data Structures, Second Edition62 STL Algorithms (continued) count: counts the occurrence of a given item in a given range; returns the number of times the value specified by the parameter occurs count_if: counts occurrences of a given value in a given range satisfying a certain criterion min: determines the minimum of two values

C++ Programming: Program Design Including Data Structures, Second Edition63 STL Algorithms (continued) max_element: determines the largest element in a given range max: determines the maximum of two values min_element: determines the smallest element in a given range for_each: access and process each element in a given range by applying a function transform: creates a sequence of elements at the destination by applying the unary operation to each element in the range

C++ Programming: Program Design Including Data Structures, Second Edition64 STL Algorithms (continued) set_intersection, set_union, set_difference, and set_symmetric_difference: assume that the elements within each range are already sorted includes: determines whether the elements in one range appear in another range set_intersection: find the elements that are common to two ranges of elements

C++ Programming: Program Design Including Data Structures, Second Edition65 STL Algorithms (continued) set_union: find the elements that are contained in two ranges of elements set_difference: finds the elements in one range that do not appear in another set_symmetric_difference: creates a sequence of sorted elements that are in one sorted range but not in another

C++ Programming: Program Design Including Data Structures, Second Edition66 STL Algorithms (continued) accumulate: finds the sum of all the elements in a given range adjacent_difference: returns an iterator positioned one past the last element copied at the destination inner_product: manipulates the elements of two ranges

C++ Programming: Program Design Including Data Structures, Second Edition67 Summary STL consists of: −Containers: class templates −Iterators: step through the elements of a container −Algorithms: manipulate the elements in a container

C++ Programming: Program Design Including Data Structures, Second Edition68 Summary Containers −Sequence: vector, deque, and list −Associative: sets, multisets, maps, and multimaps −Container adapters: stacks, queues, and priority queues

C++ Programming: Program Design Including Data Structures, Second Edition69 Summary Iterators: input, output, forward, bidirectional, and random access iterator Predicates: Boolean function objects Algorithms: nonmodifying, modifying, numerical, and heap Algorithms are overloaded for flexibility