1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.

Slides:



Advertisements
Similar presentations
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Advertisements

Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
1 STL and Its Design Principles Alexander Stepanov.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
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.
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.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
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.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
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
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.
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.
Alexander Stepanov A9.com 1 One algorithm from The Book: A tribute to Ira Pohl
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)
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 19: The STL: Containers and Iterators 1.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
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.
Overview of C++ Templates
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Intro to the C++ STL Timmie Smith September 6, 2001.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
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.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
AP/DJ AP: a generic technology
Lecture 36 OOP The STL Standard Template Library
Motivation for Generic Programming in C++
Standard Template Library
CS212: Object Oriented Analysis and Design
EECE 310: Software Engineering
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
Introduction to the Standard Template Library
A Sorted, Unique Key Container
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
STL - Algorithms.
Standard Template Library Model
the Standard Template Library
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Generic Set Algorithms
Standard Template Library
Presentation transcript:

1 Iterators & Algorithms 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 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

2 STL Algorithms We will discuss some of the STL algorithms. A good reference for them is the SGI site:

3 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);

4 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);

5 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);

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

7 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);

8 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. See examples on tlab... 1 Functions that return T or F are called predicates.

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