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.

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

. 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,
More on the STL vector list stack queue priority_queue.
Lecture 20 “Standard” Template Library. What is the Standard Template Library? A collection of C++ classes (templates) containers vectors lists stacks.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E
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.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
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.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
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)
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
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.
 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.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
The Standard Template Library Container Classes Version 1.0.
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.
Collection types CS Chakrabarti Motivation  Thus far the only collection types we have used are vector and matrix  Problem #1: given an input.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
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.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
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
Standard Template Library
C++ Templates.
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
priority_queue<T>
Doubly Linked List Implementation
Recursive Linked List Operations
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Lab4 problems More about templates Some STL
Standard Template Library
Doubly Linked List Implementation
Standard Template Library
Presentation transcript:

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

STL Standard Template Library Why use it? – Good programmers know what to write. Great ones know what to reuse. Paraphrase from “The Cathedral and the Bazaar” – A must-read for any computer scientist STL provides reusable code – Linked list, vector, map, multimap, pair, set, multiset, queue, stack, … – Don’t reinvent the wheel…

List Linked List container – No random access (does not support operator[] or at()) Essential operations – insert() – push_back() – push_front() – pop_front() – pop_back() – erase()

Set and Multiset Set – Sorted collection of unique elements – Cannot change value of an element – No random access Multiset – Allows duplicate elements Essential operations – insert() – erase() – count( element ) – find( element )

Pair – Connects two items into a single object Essential data – first gets the first member of pair – second gets the second member of pair Example pair hello( 5, “Hello”); cout << hello.second << endl;// Hello

Map and Multimap Map – Stores key/value pairs, sorted by key – Value is modifiable, key is not – Key must be unique Multimap – Allows duplicate keys Essential operations – insert() – erase() – count( key ) – find( key )

Iterators Problem – Not all STL classes provide random access – How do we do “for each element in X”? Solution – Iterators “Special” pointers “Iterate” through each item in the collection Several types – Bidirectional – Const bidirectional Why is this necessary? Why can’t we just use a normal pointer? Where have we seen these before??? What does const mean?

Iterators Essential operations – begin() Returns an iterator to first item in collection – end() Returns an iterator ONE BEYOND the last item in collection – How does this simplify things? If the collection is empty, begin() == end()

Set Example int main ( ) { set iSet; iSet.insert(4); iSet.insert(12); iSet.insert(7); // this looping construct works for all containers set ::const_iterator position; for (position = iSet.begin(); position != iSet.end(); ++position) { cout << *position << endl; } return 0; }

Map Example int main ( ) { // create an empty map using strings // as keys and floats as values map stocks; // insert some stock prices stocks.insert( make_pair("IBM", 42.50)); stocks.insert( make_pair("XYZ", 2.50)); stocks.insert( make_pair("WX", 0.50)); // instantiate an iterator for the map map ::iterator position; // print all the stocks for (position = stocks.begin(); position != stocks.end(); ++position) cout first second << " )\n"; return 0; }

Iterators - Overloaded Operators * (pointer dereference) – Dereferences the iterator ++ – Moves forward to next element -- – Moves backward to previous element == – True if two iterators point to same element != – False if two iterators point to different elements = – Assignment, makes two iterators point to same element

Iterators and Collection Methods erase( iterator ) – Parameter is an iterator Can have as many iterators into a collection as necessary

Practice Create a vector of integers Using an iterator and a loop – Change each integer to be the value of its square Using an iterator and a second loop – Print each item in reverse order

Challenge Using a map, create a collection of student grades – Key Student ID – Value Grade they want in this course Store 10 students and their desired grade Iterate through the map – Print each key/value pair in the map What sorting mechanism did the map use? – How would we specify that we wanted it sorted another way?