Iterators and STL Containers

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.
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.
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.
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.
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.
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)
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.
 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.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
The Standard Template Library Container Classes Version 1.0.
C++ Review STL CONTAINERS.
Collection types CS Chakrabarti Motivation  Thus far the only collection types we have used are vector and matrix  Problem #1: given an input.
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.
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
CS212: Object Oriented Analysis and Design
Introduction to olympic programming
Programming with ANSI C ++
Standard Template Library
C++ Templates.
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Basic Data Structures.
Chapter 22: Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Associative Structures
Containers & Iterators
priority_queue<T>
Elements are always copied when they are put into a container
Recitation Outline C++ STL associative containers Examples
Doubly Linked List Implementation
Recursive Linked List Operations
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
Lab4 problems More about templates Some STL
Standard Template Library
Doubly Linked List Implementation
Standard Template Library
An Introduction to STL.
A dictionary lookup mechanism
Presentation transcript:

Iterators and STL Containers CMSC 202

Warmup Write the class definition for the templated Bag class A bag has: Random insertion Random removal

STL Standard Template Library Why use it? STL provides reusable code 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 Essential operations 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 Multiset Essential operations 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 Pair Essential data Example 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<int, string> hello( 5, “Hello”); cout << hello.second << endl; // Hello

Map and Multimap Map Multimap Essential operations 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 Solution Not all STL classes provide random access Where have we seen these before??? 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? What does const mean?

Iterators Essential operations begin() end() 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<int> iSet; iSet.insert(4); iSet.insert(12); iSet.insert(7); // this looping construct works for all containers set<int>::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<string, float> 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<string, float>::iterator position; // print all the stocks for (position = stocks.begin(); position != stocks.end(); ++position) cout << "( " << position->first << ", " << position->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?