This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 332: C++ STL algorithms C++ STL Algorithms Generic algorithms Apply to a wide range of types E.g. , sorting integers ( int ) vs. intervals ( pair<int, int> ) Dont require inheritance relationships Types substituted need not have a common base class Need only to be models of the algorithms concept Implementations in C++ Rely on templates, interfacebased polymorphism Algorithms are implemented as function templates Use types that model iterator concepts Iterators in turn give access to containers CSE 332: C++ STL algorithms Example Revisited: Linear Search From Austern: Generic Programming and the STL Sequential (linear) search: find char c in string s char * strchr (char* s, char c) { while (*s != 0 && *s != c){ ++s; } return *s == c ? s : (char *) 0; } Problem: not very general Range of iteration is always defined up to \0 character Only works for a zero terminated string in C/C++ CSE 332: C++ STL algorithms Review in Detail: Linear Search with Ranges First generalization (Austern, pp. 11): use a range char * find1 (char* first, char* last, char c){ while ( first != last && * first != c) ++first ; return first; } Gives an explicit range (calculate its length how?) Assumes first is before last (can check how?) Note how caller checks for success changed: why? CSE 332: C++ STL algorithms General Requirements for Linear Search Before we try to improve the algorithm further Lets come up with a definition of what it needs to do This helps to plan what to require and what to leave flexible Any linear search implementation must offer a way to: Indicate the sequence over which search will occur Represent a position within the sequence Advance to the next element of the sequence Detect the end of the sequence Return a value as an indication of success or failure Goal: meet these requirements flexibly and efficiently CSE 332: C++ STL algorithms Linear Search over Parameterized Types Second generalization: use templates to parameterize the function argument types template <typename T> T * find2( T * first, T * last, const T & value){ while (first != last && *first != value) ++first; return first; } How much did the find1 code need to change? One last problem What if we want to apply this to a data structure whose ranges cant be traversed via simple pointers? CSE 332: C++ STL algorithms Linear Search with Generic Iterators Third generalization: separate iterator type parameter The STLs linear search algorithm (Austern pp. 13): template < typename Iterator, typename T> Iterator find ( Iterator first, Iterator last, const T & value) { while (first != last && *first != value) ++first; return first; } Notice how algorithm depends on the iterators Notice how refinements made algorithm more abstract but still essentially does the same thing i.e., algorithm structure (and time complexity) is the same CSE 332: C++...
View
Full
Document
 Spring '08
 Smart
 Algorithms, Inheritance, Sort

Click to edit the document details