CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

DATA STRUCTURES USING C++ Chapter 5
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.
Lists: An internal look
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Data Structures Using C++ 2E
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++ 2E
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
Object Oriented Data Structures
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Generic Programming Using the C++ Standard Template Library.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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 &
Lecture 7 : Intro. to STL (Standard Template Library)
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.
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.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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-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.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Standard Template Library
CS212: Object Oriented Analysis and Design
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.
Collections Intro What is the STL? Templates, collections, & iterators
Array Lists Chapter 6 Section 6.1 to 6.3
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Class string and String Stream Processing
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL

Recap Templates Function and Class Templates I/O streams Handling files

Outline of Lecture 23 Introduction to STL STL Components Allocators Vectors

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 IteratorAccess Allowed Random AccessStore and retrieve values. Elements may be accessed randomly. BidirectionalStore and retrieve values. Forward and backward moving. ForwardStore and retrieve values. Forward moving only. InputRetrieve, but not store values. Forward moving only. OutputStore, 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 1. Decide on the type of container to use 2. Use its member functions to add elements to the container, access or modify those elements, and delete elements 3. 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 vector

Vector: Constructors explicit vector(const Allocator &a = Allocator( ) ); explicit vector(size_type num, const T &val = T ( ), const Allocator &a = Allocator( )); vector(const vector &ob); template vector(InIter start, InIter end, const Allocator &a = Allocator( )); Constructs an empty vector 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 vector iv; vector cv(5); vector cv(5, 'x'); vector iv2(iv); // create zero-length int vector // create int vector from an int vector // create 5-element char vector // initialize a 5-element char vector

Common functions MemberDescription 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

Thank you Next Lecture: Iterators