1 Iterators Good reference site:

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.
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.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.
1 STL Algorithms Several STL algorithms require a functional argument. Consider an employee database. We may want to remove all employees who satisfy the.
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.
Writing Your Own STL Container Ray Lischner
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.
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.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
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)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
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)
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
CS342 Data Structures End-of-semester Review S2002.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 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.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 7 : Intro. to STL (Standard Template Library)
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.
Standard Template Library (STL) - Use Vector and Deque
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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)
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Object-Oriented Programming (OOP) Lecture No. 42.
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.
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.
Standard Template Library
CS212: Object Oriented Analysis and Design
COP 3530 Data Structures & Algorithms
Programming with ANSI C ++
Standard Template Library (STL)
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
CS212: Object Oriented Analysis and Design
Containers, Iterators, Algorithms, Thrust
Standard Template Library Model
Standard Template Library
Presentation transcript:

1 Iterators Good reference site:

2 Iterators We have discussed the major containers provided by the STL. In addition to these, users can write their own containers. We often need certain algorithms for different containers. For example, we want to be able to find elements in a list, or in a tree. We want to be able to sort a list or sort a vector. We may want to add up all the elements of a list or all the elements of a vector.

3 Sequences and Iterators One approach to this problem would be to write separate sort or sum or search methods for these containers. A better approach would be to write only one version of sort or sum and somehow be able to apply it to any container. This means that we must find a way to manipulate a container without knowing exactly what kind it is and how it is implemented. We focus on the notion of a sequence of elements. The sequence has a beginning and an end. We use iterators to refer to elements and tools that allow us to access the next element of the sequence. An iterator is an abstraction of the notion of a pointer to an element of a sequence. Any object that behaves like an iterator is an iterator. Iterators are the glue that holds containers and algorithms together!

4 Sequences and Iterators Sequences arrays vectors singly- and doubly-linked lists trees input output There are several types of iterators. Different types support different operations. Random access Bidirectional Forward Input/Output more powerful

5 Iterators Category Operation ReadAccessWriteIterateCompare Input Output Forward Random-Access Bi-directional *i= =*i –>–> –>–> –>–> –>–>[ ] ++ – ++ – += – = + – == != ==!= ==!= ==>= <= > <

6 Iterators Container Type Supported Random accessBi-directional vector list deque queue stack None!          

7 Iterators & Algorithms Remember: Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members of a sequence 1 Idea 1: Write a separate sum() for each type of sequence (list, vector, etc.) Idea 2: Write sum() as a function template that can take a sequence as an argument Idea 3: Write sum() as a function template that can take range-specifying iterators as arguments. 1 The STL actually provides such an algorithm: accumulate

8 STL Algorithms find() Returns the first iterator i in the range [first, last) such that *i == value, or last if no such iterator exists. template InputIterator find(InputIterator first, InputIterator last, const EqualityComparable& value);

9 STL Algorithms count() Returns the number of iterators i in [first, last) such that *i == value template iterator_traits ::difference_type count(InputIterator first, InputIterator last, const EqualityComparable& value);

10 STL Algorithms remove() Returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. template ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value);

11 STL Algorithms reverse() Reverses the specified range. template void reverse(BidirectionalIterator first, BidirectionalIterator last);

12 STL Algorithms copy() Copies elements from the range [first, last) to the range [result, result + (last - first)) template OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);

13 STL Algorithms Several STL algorithms require a functional argument. e.g. Consider our employee database. We may want to remove all employees who satisfy the requirement "the last evaluation report was very bad". A suitable function would be something like: remove_if (first, last, bad_eval()) where bad_eval() is a function 1 that examines an element's evaluation number and returns T if it is below some threshold. Or, we may want to sort all employees based on their evaluation number. We will then need to do something like sort(first, last, greaterThan()) where greaterThan() defines a comparison between employees, based on their evaluations. 1 Functions that return T or F are called predicates.

14 STL Algorithms Some functions that take predicates: find_if() remove_if() replace_if() min(), max()