C++ осень 2012 лекция 4

  • View
    129

  • Download
    3

Embed Size (px)

Transcript

  • C++

  • 4. STL

    Standard Templates Library (STL):

    .

    C++.

    STL.

    STL: , , ,

    .

  • (STL):

    (. Standard Templates Library,

    STL) 1970- 1990- . . , .

    (D. Musser) .

    C++

    .

  • STL

    . , STL

    :

    ,

    (,

    , .);

    ;

    - (

    );

    .

  • STL

    STL

    , :

    ;

    ;

    ;

    ;

    ;

    ;

    .

    , STL

    (),

    .

  • STL (1 / 2)

    STL :

    ( )

    , , ..;

    ,

    , , ..;

    ( ),

    ;

    , -

    ;

    , ,

    , ;

    .

  • STL (2 / 2)

    vector vi =

    vector(10, 1); /* {1, 1, , 1} */

    vector::iterator i = vi.begin();

    vector::iterator j = vi.end();

    search(i, j, 1);

  • STL (1 / 3)

    STL

    , ,

    -

    1 .

    =

  • STL (2 / 3)

    STL :

    : = 1 ;

    : = ;

    : = 2 ;

    :

    = log ;

    :

    = log .

    , 1 log log 2 .

    (, quicksoft 2 ).

  • STL (3 / 3)

    :

    ( );

    ,

    ,

    .

  • :

    STL ,

    , .

  • STL T, . vector, . ( , . deque double-ended queue) deque . list, , .

    : STL, C- T a[N] String.

  • :

    1

    /

    1

    1

    /

    1

    /

    1

    1

    1

  • STL , . set . multiset 2 . map Key T. multimap c 2 .

  • :

    STL

    .

  • -

    ,

    .

    : find(), equal(), count().

    -, ,

    , , , .

    : copy(), random_shuffle(), replace().

  • .

    STL,

    .

    , , -

    .

    : sort(), binary_search(), set_union().

    ,

    ,

    .

    : accumulate(), partial_sum(), inner_product().

  • :

    ( ) ,

    .

    .

  • ,

    .

    ,

    first last, .

    last :

    ;

  • : find

    template

    InputIterator find( //

    InputIterator first, //

    InputIterator last, //

    const T& value) //

    {

    while(first != last && *first != value)

    ++first;

    return first;

    }

  • : copy

    template

    OutputIterator copy(

    InputIterator first,

    InputIterator last,

    OutputIterator result)

    {

    while(first != last) {

    *result = *first;

    ++first;

    ++result;

    }

    return first;

    }

  • : replace

    template

    void replace(

    ForwardIterator first,

    ForwardIterator last,

    const T& x, const T& y)

    {

    while(first != last) {

    if(*first == x)

    *first = y;

    ++first;

    }

    return first;

    }

  • :

    ( )

    ,

    ( 0 )

    .

    STL-

    (class) (struct) -

    operator().

  • : multiplies

    template

    class multiplies :

    public binary_function

    {

    public:

    T operator()(const T& x, const T& y)

    const {

    return x * y;

    }

    };

  • :

    STL

    ,

    , .