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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 6 Queues and Deques.
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.
Standard Containers: Vectors
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
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 (STL) Overview – Part 1 Yngvi Bjornsson.
Basic C++ Sequential Container Features
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.
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.
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:
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Data Structures Using C++ 2E
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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.
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.
Useful Data Structures in C++ and Java. Useful Data Structures in C++ (1) Templates are C++’s mechanism to define abstract objects which can be parameterized.
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,
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.
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.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
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)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
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.
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.
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.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
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  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
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.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
Standard Template Library (STL)
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CS212: Object Oriented Analysis and Design
Introduction to C++ STL
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Presentation transcript:

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 other objects (elements) and provides methods for accessing its elements. Iterators Pointer-like objects used to access elements Algorithms Basic algorithms to manipulate the elements of containers. Examples include sort, find, reverse.

2 Containers Three categories of containers: Sequential (vector, deque, list) Its elements are stored in a linear order. Associative (map, set, hash, etc) Its elements are stored by (unique) key Its elements are retrieved by key. Retrieval is efficient. Supports insert/delete but not at specific locations. Adaptors (stack, queue, priority queue) Built on top of other containers.

3 Iterators An iterator is a generalization of a pointer It is used to iterate over the elements stored in a container. Usually invalidated after insert/delete operations Different classes have different types of iterators: forward (++) e.g. hash table iterators bidirectional (++/--) e.g. list iterators random access e.g. vector iterators

4 STL Vectors Similar to arrays, but number of elements can vary dynamically. Implemented like the dynamic array we've seen before Support random access Support constant time insert/delete at the end Support linear insert/delete at all other points

5 STL Vectors #include Example: vector V(3,10); // vector of integers, initially containing three 10s vector ::iterator it; // iterator to members of a vector of integers for (it = V.begin(); it != V.end(); it++) cout << *it << “ “; // note the pointer-like syntax. returns an iterator to the first element returns an iterator just past the last element Output:

6 STL Vectors vector V; // vector of integers, default capacity 0. cout << "initial capacity: " << V.capacity() << "\n"; for (int i=0; i<5; i++) { V.push_back(i); cout << "size: " << V.size() << ", capacity: " << V.capacity() << "\n"; } Output: initial capacity: 0 size: 1, capacity: 1 size: 2, capacity: 2 size: 3, capacity: 4 size: 4, capacity: 4 size: 5, capacity: 8 When the number of elements (size) is the same as the vector's capacity, and we try to insert a new element, the capacity is doubled. You should always assume that when the capacity changes (which involves memory reallocation) or when an element is removed (which changes the positions of elements), any existing iterators do not necessarily point to what they did before.

7 STL Deques Support random access Support constant time insert/delete at both endpoints Support linear time insert/delete in the middle Insert/Erase operations invalidate iterators Implementations Circular array Array of pointers to fixed-size blocks How would insert work?

8 STL Lists Doubly-linked Support fwd/bwd traversal (bidirectional iterators), but no random access Insert operation does not invalidate iterators, but delete will invalidate the iterator pointing to the element that was deleted.

9 STL Lists #include Example: list L; L.push_back(10); L.push_front(0); L.insert(L.begin()+1, 12); // insert element in 2nd position L.sort(); // a stable sorting algorithm (meaning that the // relative positions of equal elements are maintained)

10 Stacks in the STL #include Built on deques by default LIFO Structure. No iterators! Example: stack S; S.push(10); S.push(0); int x = S.top(); S.pop();

11 Queues in the STL #include FIFO structure, no iterators. Example: queue > Q; Q.push(10); Q.push(0); int s = Q.size(); while (!Q.empty()) Q.pop(); queue based on a vector, rather than the default deque. make certain there's space between the two angle brackets.

12 Adaptors Stacks and Queues are "based" on other containers. What is the relationship between a stack and a deque? The stack is NOT a deque public inheritance is wrong The stack USES a deque use private inheritance The stack implementation can use the deque's member functions to perform operations in a controlled way, but a user of the stack does not have access to the deque members and thus cannot modify the stack in an illegal way.