Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.

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

Chapter 6 Queues and Deques.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
. 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,
. 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.
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)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
C++ for Engineers and Scientists Third Edition
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 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.
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.
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)
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Intro to the C++ STL Timmie Smith September 6, 2001.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
The Standard Template Library Container Classes Version 1.0.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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
Standard Template Library
CS212: Object Oriented Analysis and Design
Data Structure By Amee Trivedi.
Programming with ANSI C ++
Standard Template Library
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CS212: Object Oriented Analysis and Design
Dynamic Data Structures and Generics
Standard Template Library
Standard Template Library
Standard Template Library
Presentation transcript:

Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits the development of general programs that do not depend on the underlying container.

Standard Template Library –The STL is composed on six types of components: Containers Iterators Generic Algorithms Function Objects Adaptors Allocators

Containers The Containers hold data for manipulation.

Sequence Containers The Sequence Containers are vector, deque, and list. vector and deque are both based upon arrays. vectors can change size dynamically and can be assigned to one another. –Essentially a resizable array. The vector class provide the best random- access performance. The vector class permits insertions and deletions at the back of the vector.

Sequence Containers The deque container is a double-ended queue and provides efficient index access to data. The deque container provides insertions and deletions at both the front and back of the queue. The list container implements a doubly linked- list. The list container provides insertions and deletions anywhere in the list.

Associative Containers The Associative Containers provide direct access to store and retrieve elements via a key. –The keys of associative containers are maintained in a sorted order. The iterator will traverse through the container in sorted order.

Associative Containers –The set container provides rapid look-up but does not permit duplicates. If an attempt is made to insert a duplicate key, the duplicate is ignored. –A multiset provides rapid look-up but does permit duplicates. –The order of data stored in a multiset is determined by a comparator function object that is specified when the container is created. typedef std::multiset > ims; ims intMultiSet;

Associative Containers –The map class provides rapid lookup using a rapid key based lookup. –The map class does not permit duplicates. –A map is commonly called an associative array. –Insertions and deletions occur anywhere in the map. –The map elements are pairs containing a key and a value.

Associative Containers –The insertion of an element into a map requires a pair object that has the key and a value. –The multimap container is the same as the map container but it permits duplicates. –A typical use of the multimap is to implement red-black binary trees.

First-Class Containers Sequence and Associative Containers are considered first-class containers. –First-class containers support iterators. –First-class containers support actual data structure implementation that store elements.

Containers Adopters Container Adopters are consider near- containers because they are similar in concept to first-class containers but do not provide all of the capabilities of first- class containers. –Container Adopters do not provide the actual data structure implementation into which elements are stored because adopters do not support iterators.

Container Adopters –Container Adopters adapt to other components for special purposes. A stack container adopter will adapt a vector, list, or deque so that the container will appear to the user as a stack that defines a LIFO data structure. –Permits the use of Push and Pop. The queue container adopter permits the insertion of data at the back of the underlying data structure and deletions from the front of the underlying data structure. –The typical FIFO data structure.

Container Adopters The priority_queue adopter provides insertions in sorted order into the data structure and deletions from the front of the data structure. –By default this priority_queue is implemented using the vector class. –Insertions occur such that the highest priority item (for example, the largest value) is inserted at the front of the queue.

Iterators Iterators provide the ability to move through containers. –Iterators are the interface between the containers and the algorithms that manipulate the data. –Iterators are implemented separately from the container in the STL. This is different from other “object-oriented” libraries. It does permit a high level of reuse but reduces the object-oriented nature of the program.

Iterators –Iterators are used as pointers to the elements in first-class containers. –The type of iterator associated with a container determines if 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. Figure 20.8 list the type of iterators permitted for each type of container.

Iterators –There are five types of iterators. Input, output, forward, bi-directional, and random access. –There are also constant iterators that can not modify the containers they are associated with. –Additional iterator variations are reverse iterators and iostream iterators.

Iterators –The operator++ is required to move through a container with a forward iterator. –The operator++ and operator– can be used to move through a container with a bi- directional iterator. –Random access iterators permit operator++, operator--, operator+, operator-, and iterator arithmetic. Ordinary pointers are automatically random access iterators.

Iterators –The begin() function returns an iterator pointing to the first element in the container. –The end() function returns an iterator pointing to the first element past the end of the container. The first element that does not exist.

Algorithms The STL provides algorithms that are used generically across the containers. –There are about 70 algorithms implemented in the STL. –Algorithms manipulate the elements of containers indirectly through the iterator. –The design of the STL algorithms, iterators, and containers avoids the overhead of virtual function calls.

Algorithms –Some algorithms manipulate sequences of elements defined in iterator pairs. The pairs contain a first iterator pointing to the first element to manipulate and the last iterator that points to one past that last element to manipulate.

Algorithms Mutating-sequence algorithms modify the containers that they are applied to. –Such algorithms include: copy(), remove(), fill(), replace(), rotate(), etc. A list of mutating-sequence algorithms can be found on page 984, Figure 20.11

Algorithms Non-mutating-sequence algorithms do not modify the containers they are applied to. –Such algorithms include: count(), equal(), find, search(), etc. A list can be found on page 985, Figure

Algorithms Numeric algorithms obviously conduct numerical manipulations of the data in the container. –The header file must be included to use these algorithms. –A list of the algorithms is found on page 985, Figure 20.13

Allocators Allocators are used to allocate and deallocate memory for data storage. –Allocators are used in place of new and delete. –Allocators are most useful on machines that have several memory models available, for example IBM PCs. –The allocators decouple the algorithms from assumptions about a particular memory model.

Example Create a vector of integers and replace certain elements.

Examples Store integers into a list. Sort integers that are stored in a vector. Sum input integer values.

Advantages of Using STL There are many benefits of using the STL: –The STL provides a significant level of flexible functionality. –The STL provides very good performance at low run-time space costs.

Advantages of Using STL –The STL only uses generalized algorithms when their efficiency is good. –The STL also provides specialized algorithms. For example, the generic sort algorithm works well on deques and vectors and could also be used for lists but the efficiency would be poor. –A special optimized sort algorithm for lists is provided.

Disadvantages of Using STL As with anything, there disadvantages of using the STL: –Programming with the STL does not increase the safety of the programs. –Programming with the STL requires to time to adjust to the differences of standard object-oriented programming. Containers, iterators, and algorithms.

Disadvantages of Using STL –Iterators have all the efficiency and drawbacks of pointers, since they generalize. The programmer is still responsible for verifying subscript bounds, dangling references, deallocation issues, etc. For example, the vector class permits the use of the operator[] but the subscripts are not checked.

Disadvantages of Using STL –The error messages provided by the compiler for STL issues are very poor. The error messages detailed all the nested templates used at the time.

List Error "/opt/SUNWspro/SC5.0/include/CC/./algorithm.cc", line 1015: Error: The operation "std::list >::iterator - std::list >::iterator" is illegal. "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: While instantiating "std::__final_insertion_sort >::iterator >(std::list >::iterator, std::list >::iterator)". "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: Instantiated from non-template code.