Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.

Slides:



Advertisements
Similar presentations
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
Advertisements

1 STL Partea 2. D. Lucanu STL – Partea 2 2 Cuprins STL C++ containere standard algoritmi clasificare exemple: liste tablouri asociative agenda telefonica.
Mutating Algorithms All the algorithms we have seen have not modified the containers Some algorithms do modify values They do not modify iterators, only.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
. 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.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
Templates & STL Instructor: 小黑. Templates  Template serves as a class outline, from which specific classes are generated at compile time.  One template.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (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.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
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.
Templates and Polymorphism Generic functions and classes
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
STL !!!generic programming!!! Anar Manafov
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators.
1 CSC 262 Programming in C++ II Sykes Day 18.  We’ve repeatedly emphasized the importance of software reuse.  Recognizing that many data structures.
STL Algorithms algorithms independent of containers.
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.
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,
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.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
Introduction to C & C++ Lecture 6 – STL JJCAO.
CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms –Apply to a wide range of types E.g., sorting integers (long) or intervals (long, long)‏
Generic Programming Using the C++ Standard Template Library.
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.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
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)
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Generic Programming in C++. Generic Parameters Function for squaring a number: Function for squaring a number: sqrt(x) { return x * x; } C version: C.
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
STL Algorithms in Action STL Algorithms and their everyday application Michael VanLoon CPPcon 2015.
Introduction to the ISO C++ Standard Template Library (STL)
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Intro to the C++ STL Timmie Smith September 6, 2001.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
Overview of STL Function Objects
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
STL Overview - by Mark Bartosik October 2000 STL Overview Intended audience: C/C++ programmers not already expert in STL. Visual Basic and Java programmers.
Introduction to Templates and Standard Template Library 1.
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.
Lecture 36 OOP The STL Standard Template Library
CNS 3370 Algorithms.
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Lecture 7-3 : STL Algorithms
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Containers, Iterators, Algorithms, Thrust
Standard Template Library
Standard Template Library
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.

Generic Algorithms Templates Can process homogeneous sequences of any type –arrays, vectors, lists, anything that meets STL requirements must provide iterators pointers are iterators Lots of them!

A First Look CopyInts.cpp CopyStrings.cpp How does copy( ) work?

A First Try at copy( ) template void copy(T* begin, T* end, T* dest) { while(begin != end) *dest++ = *begin++; }

A Vector example vector iterators are necessarily not real pointers But CopyVector.cpp works!

A Second Try at copy( ) template void copy(Iterator begin, Iterator end, Iterator dest) { while(begin != end) *dest++ = *begin++; } The compiler infers the actual type of iterator As long as it supports !=, ++, and *, all is well!

Non-mutating Algorithms for_each find find_if find_first_of adjacent_find count count_if mismatch equal search find_end search_n

Mutating Algorithms transform copy copy_backward swap iter_swap swap_ranges replace replace_if replace_copy replace_copy_if fill fill_n generate generate_n remove remove_if remove_copy remove_copy_if unique reverse reverse_copy rotate rotate_copy random_shuffle

Ordering Algorithms Sorting sort stable_sort partial_sort partial_sort_copy nth_element merge inplace_merge partition stable_partition Set Operations includes set_union set_intersection set_difference set_symmetric_difference Heap Operations push_heap pop_heap make_heap sort_heap

Ordering Algorithms continued... Searching binary_search lower_bound upper_bound equal_range Permutations next_permutation prev_permutation Min/max min max min_element max_element lexicographical_compare

Predicates Functions that return a bool Many algorithms have alternate versions that apply predicates to sequence elements –for selection, deletion, etc. Examples: –CopyInts2.cpp –CopyStrings2.cpp –ReplaceStrings.cpp

Stream Iterators Facilitates reading/writing a sequence from/to a stream –without you doing a loop explicitly ostream_iterator (ostream&, const string& sep) –Examples: CopyInts3.cpp, CopyIntsToFile.cpp istream_iterator (istream&) –Example: CopyIntsFromFile.cpp

Function Objects Any class that overloads operator( ) Can take any number of arguments Typically unary or binary Example: GreaterThanN.cpp

Standard Function Objects Predicates equal_to not_equal_to greater less greater_equal less_equal logical_and logical_or logical_not Arithmetic plus minus multiplies divides modulus negate Binders binder1st binder2nd Member-related mem_fun_t mem_fun1_t mem_fun_ref_t mem_fun1_ref_t Negaters unary_negate binary_negate Pointer-related pointer_to_unary_function pointer_to_binary_function

Using a Standard Function Object #include using namespace std; int main() { greater g; cout << g(3, 4) << endl; // Prints 0 (for false) cout << g(5, 4) << endl; // Prints 1 (for true) }

Function Object Adaptors Allow combining function objects in useful ways Binders, for example: –allow you to treat a binary function as a unary function by fixing (binding a fixed value to) one of the parameters –bind2nd( ) creates a function object that stores the function and the fixed 2 nd argument –It overloads operator( ) so you can provide the missing first argument –Examples: CopyInts4.cpp, CountNotEqual.cpp, FBinder.cpp

Selected Examples MemFun1.cpp MemFun2.cpp MemFun3.cpp

Function Composition Left out of the standard –will find its way in for C++0x via generalized binders Fun to do yourself, anyway! ComposeTry.cpp ComposeFinal.cpp

Exercises Write an expression with standard function objects and adapters that searches a sequence of integers for the first element that is not less than 100. Exercise 3, 10 (in book)