C++_algorithms - Generic algorithms C STL Algorithms Apply...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
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) Don’t require inheritance relationships Types substituted need not have a common base class Need only to be models of the algorithm’s concept Implementations in C++ Rely on templates, interface-based polymorphism Algorithms are implemented as function templates Use types that model iterator concepts Iterators in turn give access to containers
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++   STL algorithms Example from Last Time: Linear Search From Austern: “Generic Programming and the STL” Sequential (linear) search: find  char c  in  string s   char * strchr (char* s, char 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++ 
Background image of page 2
CSE 332: C++   STL algorithms Where We Stopped: 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?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++   STL algorithms General Requirements for Linear Search Before we try to improve the algorithm further Let’s 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
Background image of page 4
  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, value){ ++first; return first; } How much did the  find1  code need to change?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

C++_algorithms - Generic algorithms C STL Algorithms Apply...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online