Object-Oriented Programming (OOP) Lecture No. 42.

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.
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.
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.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
More on the STL vector list stack queue priority_queue.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Templates and the STL.
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
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:
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
 2003 Prentice Hall, Inc. All rights reserved stack Adapter stack –Header –Insertions and deletions at one end –Last-in, first-out (LIFO) data.
STL !!!generic programming!!! Anar Manafov
Data Structures Using C++ 2E
Containers Overview and Class Vector
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
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.
Chapter 19 Standard Template Library Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
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.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Intro to the C++ Std Library.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
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.
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:
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.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 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.
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.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
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 in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
Introduction to Templates and Standard Template Library 1.
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 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
abstracting away the data structure
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Iterators and STL Containers
STL Библиотека стандартных шаблонов
Standard Template Library
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 42

Iterators Iterators are types defined by STL Iterators are for containers like pointers are for ordinary data structures STL iterators provide pointer operations such as * and ++

Iterator Categories Input Iterators Output Iterators Forward Iterators Bidirectional Iterators Random-access Iterators

Input Iterators Can only read an element Can only move in forward direction one element at a time Support only one-pass algorithms

Output Iterators Can only write an element Can only move in forward direction one element at a time Support only one-pass algorithms

Forward Iterators Combine the capabilities of both input and output iterators In addition they can bookmark a position in the container

Bidirectional Iterators Provide all the capabilities of forward iterators In addition, they can move in backward direction As a result they support multi-pass algorithms

Random Access Iterators Provide all the capabilities of bidirectional iterators In addition they can directly access any element of a container

Iterator Summary Input Output Forward Random Access Bidirectional

Container and Iterator Types Sequence Containers -- vector-- random access -- deque-- random access -- list-- bidirectional Associative Containers -- set-- bidirectional -- multiset-- bidirectional -- map-- bidirectional -- multimap-- bidirectional

…Container and Iterator Types Container Adapters -- stack-- (none) -- queue-- (none) -- priority_queue-- (none)

Iterator Operations

All Iterators ++p –pre-increment an iterator p++ –post-increment an iterator

Input Iterators *p –Dereference operator (used as rvalue) p1 = p2 –Assignment p1 == p2 –Equality operator p1 != p2 –Inequality operator p-> –Access Operator

Output Iterators *p –Dereference operator (can be used as lvalue) p1 = p2 –Assignment

Forward Iterators Combine the operations of both input and output iterators

Bidirectional Iterators Besides the operations of forward iterators they also support --p –Pre-increment operator p-- –post-decrement operator

Random-access Iterators Besides the operations of bidirectional iterators, they also support p + i –Result is an iterator pointing at p + i p – i –Result is an iterator pointing at p – i

…Random-access Iterators p += i –Increment iterator p by i positions p –= i –Decrement iterator p by i positions p[ i ] –Returns a reference of element at p + i p1 < p2 –Returns true if p1 is before p2 in the container

…Random-access Iterators p1 <= p2 –Returns true if p1 is before p2 in the container or p1 is equal to p2 p1 > p2 –Returns true if p1 is after p2 in the container p1 >= p2 –Returns true if p1 is after p2 in the container or p1 is equal to p2

Example – Random Access Iterator typedef std::vector IntVector; int main() { const int SIZE = 3; int iArray[ SIZE ] = { 1, 2, 3 }; IntVector iv(iArray, iArray + SIZE); IntVector::iterator it = iv.begin(); cout << “Vector contents: ”; for ( int i = 0; i < SIZE; ++i ) cout << it[i] << ", "; return 0; }

…Sample Output Vector contents: 1, 2, 3,

Example – Bidirectional Iterator typedef std::set IntSet; int main() { const int SIZE = 3; int iArray[ SIZE ] = { 1, 2, 3 }; IntSet is( iArray, iArray + SIZE); IntSet::iterator it = is.begin(); cout << “Set contents: ”; for (int i = 0; i < SIZE; ++i) cout << it[i] << ", "; // Error return 0; }

…Example – Bidirectional Iterator typedef std::set IntSet; int main() { const int SIZE = 3; int iArray[ SIZE ] = { 1, 2, 3 }; IntSet is( iArray, iArray + SIZE); IntSet::iterator it = is.begin(); cout << “Set contents: ”; for ( int i = 0; i < SIZE; ++i ) cout << *it++ << ", ";// OK return 0; }

…Sample Output Set contents: 1, 2, 3,

…Example – Bidirectional Iterator typedef std::set IntSet; int main() { const int SIZE = 3; int iArray[ SIZE ] = { 1, 2, 3 }; IntSet is( iArray, iArray + SIZE); IntSet::iterator it = is.end(); cout << “Set contents: ”; for (int i = 0; i < SIZE; ++i) cout << *--it << ", "; return 0; }

…Sample Output Set contents: 3, 2, 1,

Example – Input Iterator #include using std::cin; using std::cout; using std::endl; #include int main() { int x, y, z; cout << "Enter three integers:\n";

…Example – Input Iterator std::istream_iterator inputIt( cin ); x = *inputIt++; y = *inputIt++; z = *inputIt; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; return 0; }

…Example – Input Iterator int main() { int x = 5; std::istream_iterator inputIt( cin ); *inputIt = x;// Error return 0; }

Example – Output Iterator int main() { int x = 1, y = 2, z = 3; std::ostream_iterator outputIt( cout, ", " ); *outputIt++ = x; *outputIt++ = y; *outputIt++ = z; return 0; }

…Example – Output Iterator int main() { int x = 1, y = 2, z = 3; std::ostream_iterator outputIt( cout, ", " ); x = *outputIt++;// Error return 0; }

Algorithms STL includes 70 standard algorithms These algorithms may use iterators to manipulate containers STL algorithms also work for ordinary pointers and data structures

…Algorithms An algorithm works with a particular container only if that container supports a particular iterator category A multi-pass algorithm for example, requires bidirectional iterator(s) at least

Examples

Mutating-Sequence Algorithms copy copy_backward fill fill_n generate generate_n iter_swap partition …

Non-Mutating-Sequence Algorithms adjacent_find count count_if equal find find_each find_end find_first_of …

Numeric Algorithms accumulate inner_product partial_sum adjacent_difference

Example – copy Algorithm #include using std::cout; #include typedef std::vector IntVector; int main() { int iArray[] = {1, 2, 3, 4, 5, 6}; IntVector iv( iArray, iArray + 6 );

…Example – copy Algorithm std::ostream_iterator output( cout, ", " ); std::copy( begin, end, output ); return 0; }

Output 1, 2, 3, 4, 5, 6,

Example – fill Algorithm #include using std::cout; using std::endl; #include typedef std::vector IntVector; int main() { int iArray[] = { 1, 2, 3, 4, 5 }; IntVector iv( iArray, iArray + 5 );

…Example – fill Algorithm std::ostream_iterator output( cout, ", " ); std::copy( iv.begin(), iv.end(), output ); std::fill(iv.begin(), iv.end(), 0); cout << endl; std::copy( iv.begin(), iv.end(), output ); return 0; }