tailieunhanh - Lecture Programming principles and practice using C++: Chapter 21 - Bjarne Stroustrup

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 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 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 (., --, +, 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((), (), 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((), (), ); // 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, ); // sum the ints in a double // popular idiom, use the variable you want the result in as the initializer: double

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.