1 / 8

Algorithms and Functionals

Algorithms and Functionals. Standard Template Library. Section 1.6.4, class notes, STL references. 1. Standard Template Library Features. Algorithms Functionals Containers Iterators. Algorithms. Include <algorithm> find , for_each , sort , min , max , etc Example: Lec10/alg.cpp.

opal
Télécharger la présentation

Algorithms and Functionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and Functionals Standard Template Library Section 1.6.4, class notes, STL references 1

  2. Standard Template Library Features • Algorithms • Functionals • Containers • Iterators

  3. Algorithms Include <algorithm> find, for_each, sort, min, max, etc Example: Lec10/alg.cpp PrintArray(A,ASize); sort(A, A+ASize); PrintArray(A,ASize); reverse(A, A+ASize); PrintArray(A,ASize); } void PrintArray(int *A, int Size) { for(int i=0; i< Size; i++) cout << A[i] << " "; } int main() { const int ASize = 8; int A[ASize] = {32, 4, 8, 62, 3, 42, 23, 9}; Output 32 4 8 62 3 42 23 9 3 4 8 9 23 32 42 62 62 42 32 23 9 8 4 3 3

  4. for_each See files included from: /usr/local/include/c++/4.3.2/algorithm template<typename _InputIterator, typename _Function> _Function for_each(_InputIterator __first, _InputIterator __last, _Function __f) { __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_requires_valid_range(__first, __last); for (; __first != __last; ++__first) __f(*__first); return __f; } 4

  5. for_each Simplified template<typename _InputIterator, typename _Function> _Function for_each(_InputIterator __first, _InputIterator __last, _Function __f) { __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_requires_valid_range(__first, __last); for (; __first != __last; ++__first) __f(*__first); return __f; } template<typename I, typename F> F for_each(I first, I last, F f) { for (; first != last; ++first) f(*first); return f; } 5

  6. for_each Example • Lec10/fo.cpp • print is a function class • It overloads operator() class print { public: void operator()(int a) { cout << a << " "; } }; for_each(A, A+ASize, print()); cout << "\n"; sort(A, A+ASize); for_each(A, A+ASize, print()); cout << "\n"; reverse(A, A+ASize); for_each(A, A+ASize, print()); cout << "\n"; 6

  7. Another Function Class Example • Lec10/fo2.cpp • Use a function object to sort in descending order class gt { public: bool operator()(int a, int b) { return a > b; } }; for_each(A, A+ASize, print()); cout << "\n"; sort(A, A+ASize, gt()); for_each(A, A+ASize, print()); cout << "\n"; Output 32 4 8 62 3 42 23 9 62 42 32 23 9 8 4 3 7

  8. Functionals • They are function classes provided by STL • Include <funtional> • less, greater, etc • Example: Lec10/fo3.cpp for_each(A, A+ASize, print()); cout << "\n"; sort(A, A+ASize, greater<int>()); for_each(A, A+ASize, print()); cout << "\n"; Output 32 4 8 62 3 42 23 9 62 42 32 23 9 8 4 3 8

More Related