ITERATORS Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.

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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.
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 Using C++ 2E
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
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)
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
PROGRAMMING 1 – REPORT ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
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. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
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.
C++ Standard Template Library
CS212: Object Oriented Analysis and Design
Concepts of Programming Languages
COP 3530 Data Structures & Algorithms
Standard Template Library
CSCE 210 Data Structures and Algorithms
C++ Standard Library.
Maps, Hash tables, Skip Lists– Interesting problems
Standard Template Library
Standard Template Library (STL)
STL Common tools for C++.
Chapter 10.1 Binary Search Trees
C++ Programming Standard Library
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
18 – Sequential Containers
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
abstracting away the data structure
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Vectors the better arrays.
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Ch. 8 Priority Queues And Heaps
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Standard Template Library Model
STL: Traversing a Vector
CSE 303 Lecture 25 Loose Ends and Random Topics
STL Iterators Separating Container from Data Access.
The Standard Template Library
Standard Template Library
Iterators and STL Containers
STL List.
Collections Intro What is the STL? Templates, collections, & iterators
Vectors the better arrays.
An Introduction to Programming though C++
RANDOM NUMBERS SET # 1:
STL List.
Presentation transcript:

ITERATORS Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny

Standard Template Library A general purpose library of data structures (containers) and algorithms communicating through iterators. Plug each piece together and use in different applications. #include <vector> #include <algorithm> int main(){ int myArray[4]={2, 8, 5, 3}; std::vector<int> v(myArray, myArray+4); std::sort(v.begin(), v.end()); } vector sort container generic algorithm iterator

Containers Container is an object storing other objects which can be accessed by iterators Let 𝑋 be a container, then some important associated types: X::value_type – type of data stored in the container X::iterator – type of iterator for the container X::const_iterator …

Containers Valid expressions 𝑎.begin – beginning of container 𝑎.end – one past the last element of container 𝑎.size – size of container 𝑎.empty – does the container have anything in it?

Iterators sort Iterators are used to point to other objects. Iterators play an interface between data structures and algorithms Main operations advance(), next(), operator++ Dereference – operator∗(), operator→() vector sort container generic algorithm iterator

Iterators Forward Iterator – Only allows traversal from begin to end Bidirectional Iterator – Allows traversal in both directions (reversed order) Random Access Iterator – arbitrarily access elements in any order etc. Algorithms are defined for certain types of iterators, i.e., sort is only defined for random access iterating

Example Create a vector containing integer values int myArray[4]={2, 8, 5, 3}; std::vector<int> v(myArray, myArray+4); Iterate over the vector and output each integer for(vector<int>::iterator it = v.begin(); i != v.end(); ++it) cout<<*(it); Iterate over the vector using random access iterators to output each value for(size_t i = 0; i < v.size(); ++i) cout<<*(v.begin() + i);