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.

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.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
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.
List Representation - Chain Fall 2010 CSE, POSTECH.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
Basic C++ Sequential Container Features
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
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:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Policy Adaptors and the Boost Iterator Adaptor Library David Abrahams, Jeremy Siek, Indiana University.
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.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Linear List Array representation Data structures A data structure is a particular way of storing and manipulating data in a computer so that it can be.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Overview of C++ Templates
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
Overview of STL Function Objects
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
Reminder: Course Policy
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 16-2 Linked Structures
Linked Lists.
The Class arrayList General purpose implementation of linear lists.
C++ Functions, Classes, and Templates
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Data structures in C++.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Lists - I The List ADT.
Lists - I The List ADT.
The Class chain.
The Class chain.
List Iterator Implementation
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

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 C++ standard library. The standard library uses iterators to expose elements in a range, in a consistent, familiar way. Anything that implements this set of behaviors is called an iterator. Allows Generic Algorithms Easy to implement your own iterators and have them integrate smoothly with the standard library.

Iterator Pattern The iterator pattern describes a set of requirements that allows a consumer of some data structure to access elements in it with a familiar interface, regardless of the internal details of the data structure. The C++ standard library containers (data structures) supply iterator interfaces, which makes them convenient to use and interoperable with the standard algorithms. The iterator pattern defines a handful of simple requirements. An iterator should allow its consumers to: Move to the beginning of the range of elements Advance to the next element Return the value referred to, often called the referent Interrogate it to see if it is at the end of the range

Iterators in C++ The C++ standard library provides iterators for the standard containers (for example, list, vector, deque, and so on) and a few other noncontainer classes. You can use an iterator to print the contents of, for example, a vector like this: vector v; // fill up v with data... for (vector ::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << endl; }

C++ Iterators C++ iterators permit the same operations as the iterator pattern requires, but not literally. It's all there: move to the beginning, advance to the next element, get the referent, and test to see if you're at the end. In addition, different categories of iterators support additional operations, such as moving backward with the decrement operators (--it or it--), or advancing forward or backward by a specified number of elements.

Iterator Types 5 main types of Iterators in C++ Read only Write only Forward Iterator Reverse or Backwards Iterator Random Access Iterator With exception of Read and Write, as we go down every iterator is a superset of the previous one in terms of functionality. Common e.g. -> Pointers are a type of random access iterators.

Forward Iterators Essentially only need to traverse over elements However to make STL – compliant, or to be able to interface with STL Algorithms, an iterator over a datastructure needs to implement the following functionality Required Functionality (Forward Iterator) Assignment Tests for Equality Forward advancement using the prefix and postfix forms of the ++ operator dereferencing that returns an rvalue (value) or an lvalue (address)

Case Study – Singly Linked Chainlist #include using namespace std; template class linearList { public: virtual ~linearList() {}; virtual bool empty() const = 0; virtual int size() const = 0; virtual T& get(int theIndex) const = 0; virtual int indexOf(const T& theElement)const = 0; virtual void erase(int theIndex) = 0; virtual void insert(int theIndex,const T& theElement) = 0; //virtual void output(ostream& out) const = 0; }; template struct chainNode { // data members T element; chainNode * next; // constructors come here };

Chainlist class template class chain : public linearList { public: //constructors & destructors chain(int); ~chain(); T& get(int theIndex) const; int indexOf(const T& theElement) const; void erase(int theIndex); void insert(int theIndex,const T& theElement); void traverse(); bool empty() const {return listSize==0;} int size() const {return listSize;} protected: bool checkIndex(int theIndex) const; chainNode * firstNode; int listSize; };

Iterator Class //Iterator Class class Iterator : public std::iterator //For STL-Tagging Reasons - ignore { private: chainNode * Node; public: Iterator(chainNode * p){Node = p;} ~Iterator() {} // The assignment and logical operators Iterator& operator=(const Iterator& other) { Node = other.Node; return(*this); } bool operator==(const Iterator& other) { return(Node == other.Node); } bool operator!=(const Iterator& other) { return(Node != other.Node); }

Iterator Class - continued Iterator& operator++() { if (Node != NULL) { Node = Node->next; } return(*this); } Iterator& operator++(int) { Iterator tmp(*this); ++(*this); return(tmp); } // Return a reference to the value in the node. Do this instead // of returning by value so node can be updated directly T& operator*() { return(Node->element); } // Return the address of the value referred to. T* operator->() { return(&*(chain ::Iterator)*this); } };

New members for Chainlist Iterator begin() { return(Iterator(firstNode)); } Iterator end() { return(Iterator(NULL)); } Aaaaaaand We are DONE!!! Remember: Iterator class is public member of Chainlist class.

Usage Case: Traverse function template void chain ::traverse() { for (chain ::Iterator it = begin();it != end(); ++it) { cout<<*it<<endl; } NOTICE the standard FOR LOOP ITERATION METHOD Power of Iterators: Same method for traversing REGARDLESS of underlying data structure!!!!

References Wikipedia What is an iterator in C++ part 1 by Oreillynet.com UF CISE COP3530 Class Slides