Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Chapter 6 Queues and Deques.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Vectors, lists and queues
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Beginning C++ Through Game Programming, Second Edition
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
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.
Basic C++ Sequential Container Features
Programming Logic and Design Fourth Edition, Comprehensive
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Object Oriented Data Structures
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
Programming Interest Group Tutorial Two Data Structures.
Design and Analysis of Algorithms CSC201 Shahid Hussain 1.
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Generic Programming Using the C++ Standard Template Library.
C++ STL CSCI 3110.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
Data structures Abstract data types Java classes for Data structures and ADTs.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
CS342 Data Structures End-of-semester Review S2002.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
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.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
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.
18 Chapter Stacks and Queues
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types (ADTs)
STL Библиотека стандартных шаблонов
Tenth step for Learning C++ Programming
Standard Template Library
Presentation transcript:

Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map

Time for Various Operations

Vectors Generalization of arrays Efficient, random-access to elements High-level operations such as increasing/decreasing size of vector

Strings In one sense, a vector of characters In another sense, a completely different high-level data type Lots of string-specific operations (more later)

Lists Arbitrary size, memory used efficiently as the list grows and shrinks Sequential access only, constant access to first or last element Efficient insertion or removal at any location

Deque – Double Ended Queue Grows or shrinks as necessary Efficient insertion or removal from either end Random access to elements

Stacks and Queues Special form of deque Stack = Last in First out (LIFO) Queue = First in First out (FIFO)

Sets Ordered collection Efficient (logarithmic) insertion, removal, and test for inclusion Efficient merge, union, difference, and other set operations Multiset allows more than one entry with the same value

Priority Queue Efficient (logarithmic) insertion of new values Efficient access to largest (or smallest) value –Constant time access –Logarithmic removal

Map (Dictionary) Collection of (key, value) pairs Keys can be any ordered data type (e.g. string) Values can be any data type Efficient insertion, removal, and test for inclusion

Selecting a Container Class How are values going to be accessed? –Random – vector or deque –Ordered – set or map –Sequential – list Is the order in which values are maintained in the collection important? –Ordered – set or map –Can be sorted – vector or deque –Insertion time dependant – stack or queue

Selecting a Container Class (cont) Will the size of the structure vary widely over the course of execution? –Yes – list or set –No – vector or deque Is it possible to estimate the size of the collection? –Yes – vector

Selecting a Container Class (cont) Is testing to see whether a value is contained in the collection a frequent operation? –Yes – set Is the collection indexed? –Index values are integers – vector or deque –Index values are not integers – map

Selecting a Container Class (cont) Can values be related to each another? –Sets require relational operators –Vectors and lists do not require relational operators Is finding and removing the largest (or smallest) value from the collection a frequent operation? –Yes – priority queue

Selecting a Container Class (cont) At what positions are items inserted into and removed from the collection? –Middle – list –End – stack or queue Is a frequent operation the merging of two or more sequences into one? –Ordered – set –Unordered – list

Container Class - Examples Problem: want to read a word and print it out backwards Answer: use a stack –Read the word one letter at a time –Push each letter onto a stack as it is read –Until the last letter is read –Pop a letter from the stack and print it out –Until the stack is empty

Container Class – Examples (cont) Pseudocode: Declare a stack of characters while (there are more characters) read a character and push it onto the stack while (the stack is not empty) print the character on top of the stack and pop it off

Container Class – Examples (cont) #include using namespace std; int main() { char c; stack s; cin.get(c); while (!cin.eof()) { s.push(c); cin.get(c); } while (!s.empty()) { cout << s.top(); s.pop(); }