Đang chuẩn bị liên kết để tải về tài liệu:
Lecture Programming principles and practice using C++: Chapter 21 - Bjarne Stroustrup

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

This chapter includes contents: Algorithms and parameterization revisited, associative containers (map, set), standard algorithms (copy, sort, input iterators and output iterators,.), list of useful facilities (headers, algorithms, containers, function objects). | Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup www.stroustrup.com/Programming Abstract This talk presents the idea of STL algorithms and introduces map as an example of a container. Stroustrup/Programming Nov'13 Overview Common tasks and ideals Containers, algorithms, and iterators The simplest algorithm: find() Parameterization of algorithms find_if() and function objects Sequence containers vector and list Algorithms and parameterization revisited Associative containers map, set Standard algorithms copy, sort, Input iterators and output iterators List of useful facilities Headers, algorithms, containers, function objects Stroustrup/Programming Nov'13 Basic model A pair of iterators defines a sequence The beginning (points to the first element – if any) The end (points to the one-beyond-the-last element) begin: end: An iterator is a type that supports the “iterator operations” of ++ Point to the next element * Get the element value == Does this iterator . | Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup www.stroustrup.com/Programming Abstract This talk presents the idea of STL algorithms and introduces map as an example of a container. Stroustrup/Programming Nov'13 Overview Common tasks and ideals Containers, algorithms, and iterators The simplest algorithm: find() Parameterization of algorithms find_if() and function objects Sequence containers vector and list Algorithms and parameterization revisited Associative containers map, set Standard algorithms copy, sort, Input iterators and output iterators List of useful facilities Headers, algorithms, containers, function objects Stroustrup/Programming Nov'13 Basic model A pair of iterators defines a sequence The beginning (points to the first element – if any) The end (points to the one-beyond-the-last element) begin: end: An iterator is a type that supports the “iterator operations” of ++ Point to the next element * Get the element value == Does this iterator point to the same element as that iterator? Some iterators support more operations (e.g., --, +, and [ ]) Stroustrup/Programming Nov'13 Accumulate (sum the elements of a sequence) template T accumulate(In first, In last, T init) { while (first!=last) { init = init + *first; ++first; } return init; } 1 4 3 2 v: int sum = accumulate(v.begin(), v.end(), 0); // sum becomes 10 Stroustrup/Programming Nov'13 Accumulate (sum the elements of a sequence) void f(vector& vd, int* p, int n) { double sum = accumulate(vd.begin(), vd.end(), 0.0); // add the elements of vd // note: the type of the 3rd argument, the initializer, determines the precision used int si = accumulate(p, p+n, 0); // sum the ints in an int (danger of overflow) // p+n means (roughly) &p[n] long sl = accumulate(p, p+n, long(0)); // sum the ints in a long double s2 = accumulate(p, p+n, 0.0); // sum the ints in a double // popular idiom, use the variable you want the result in as the initializer: double