algorithms cns 3370 copyright 2003, fresh sources, inc
Post on 19-Jan-2018
212 Views
Preview:
DESCRIPTION
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<typename T> 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<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!
Non-mutating Algorithms
for_eachfindfind_iffind_first_ofadjacent_findcount
count_ifmismatchequalsearchfind_endsearch_n
Mutating Algorithms
transformcopycopy_backwardswapiter_swapswap_rangesreplacereplace_ifreplace_copyreplace_copy_iffillfill_n
generategenerate_nremoveremove_ifremove_copyremove_copy_ifuniquereversereverse_copyrotaterotate_copyrandom_shuffle
Ordering Algorithms
Sortingsortstable_sortpartial_sortpartial_sort_copynth_elementmergeinplace_mergepartitionstable_partition
Set Operationsincludesset_unionset_intersectionset_differenceset_symmetric_difference
Heap Operationspush_heappop_heapmake_heapsort_heap
Ordering Algorithmscontinued...
Searchingbinary_searchlower_boundupper_boundequal_range
Permutationsnext_permutationprev_permutation
Min/maxminmaxmin_elementmax_elementlexicographical_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<T>(ostream&, const string& sep)– Examples: CopyInts3.cpp, CopyIntsToFile.cpp
• istream_iterator<T>(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
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
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)}
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
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)
top related