algorithms cns 3370 copyright 2003, fresh sources, inc

19
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.

Upload: ella-patrick

Post on 19-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Algorithms

CNS 3370

Copyright 2003, Fresh Sources, Inc.

Page 2: 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!

Page 3: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

A First Look

• CopyInts.cpp• CopyStrings.cpp• How does copy( ) work?

Page 4: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

A First Try at copy( )

• template<typename T> void copy(T* begin, T* end, T* dest) { while(begin != end) *dest++ = *begin++;}

Page 5: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

A Vector example

• vector iterators are necessarily not real pointers• But CopyVector.cpp works!

Page 6: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

A Second Try at copy( )

• template<typename Iterator>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!

Page 7: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Non-mutating Algorithms

for_eachfindfind_iffind_first_ofadjacent_findcount

count_ifmismatchequalsearchfind_endsearch_n

Page 8: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Mutating Algorithms

transformcopycopy_backwardswapiter_swapswap_rangesreplacereplace_ifreplace_copyreplace_copy_iffillfill_n

generategenerate_nremoveremove_ifremove_copyremove_copy_ifuniquereversereverse_copyrotaterotate_copyrandom_shuffle

Page 9: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Ordering Algorithms

Sortingsortstable_sortpartial_sortpartial_sort_copynth_elementmergeinplace_mergepartitionstable_partition

Set Operationsincludesset_unionset_intersectionset_differenceset_symmetric_difference

Heap Operationspush_heappop_heapmake_heapsort_heap

Page 10: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Ordering Algorithmscontinued...

Searchingbinary_searchlower_boundupper_boundequal_range

Permutationsnext_permutationprev_permutation

Min/maxminmaxmin_elementmax_elementlexicographical_compare

Page 11: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

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

Page 12: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Stream Iterators

• Facilitates reading/writing a sequence from/to a stream– without you doing a loop explicitly

• ostream_iterator<T>(ostream&, const string& sep)– Examples: CopyInts3.cpp, CopyIntsToFile.cpp

• istream_iterator<T>(istream&)– Example: CopyIntsFromFile.cpp

Page 13: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Function Objects

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

Page 14: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Standard Function Objects

Predicatesequal_tonot_equal_togreaterlessgreater_equalless_equallogical_andlogical_orlogical_not

Arithmeticplusminusmultipliesdividesmodulusnegate

Bindersbinder1stbinder2nd

Member-relatedmem_fun_tmem_fun1_tmem_fun_ref_tmem_fun1_ref_t

Negatersunary_negatebinary_negate

Pointer-relatedpointer_to_unary_functionpointer_to_binary_function

Page 15: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Using a Standard Function Object

#include <functional>#include <iostream>using namespace std;

int main() { greater<int> g; cout << g(3, 4) << endl; // Prints 0 (for false) cout << g(5, 4) << endl; // Prints 1 (for true)}

Page 16: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

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 2nd argument

– It overloads operator( ) so you can provide the missing first argument

– Examples: CopyInts4.cpp, CountNotEqual.cpp, FBinder.cpp

Page 17: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

Selected Examples

• MemFun1.cpp• MemFun2.cpp• MemFun3.cpp

Page 18: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

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

Page 19: Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc

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)