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

  • View
    180

  • Download
    1

Embed Size (px)

Transcript

  • C++

  • 5. STL

    STL: .

    STL.

    :

    .

    .

    :

    -.

    4.

  • :

    ( ) ,

    .

  • :

    ( ):

    STL , ;

    ;

    .

  • C++

    C++

    STL.

    :

    const int N = 100;

    int a[N], b[N];

    // ...

    copy(&a[0], &a[N], &b[0]);

    replace(&a[0], &a[N / 2], 0, 42);

  • : (1 / 2)

    STL

    (,

    )

    ():

    InputIterator

    OutputIterator

    ForwardIterator

    BidirectionalIterator

    RandomAccessIterator

  • : (2 / 2)

    :

    template

    OutputIterator copy(InputIterator first,

    InputIterator last,

    OutputIterator result);

  • :

    STL

    :

    (

    (. ); *i ):

    Container::iterator

    (*i ):

    Container::const_iterator

    :

    Container::reverse_iterator

    :

    Container::const_reverse_iterator

  • T a[N] T* ,

    T a[N] const T* ,

    vector vector::iterator ,

    vector vector::

    const_iterator

    ,

    deque deque::iterator ,

    deque deque::

    const_iterator

    ,

    list list::iterator ,

    list list::

    const_iterator

    ,

  • set set::iterator ,

    set set::const_iterator ,

    multiset multiset::iterator ,

    multiset multiset::

    const_iterator

    ,

    map map::iterator ,

    map map::

    const_iterator

    ,

    multimap

    multimap::

    iterator

    ,

    multimap

    multimap::

    const_iterator

    ,

  • (1 / 3)

    ,

    *i

    .

    , STL

    ,

    Container:

    back_insert_iterator

    Container::push_back;

    front_insert_iterator

    Container::push_front;

    insert_iterator

    Container::insert.

  • (2 / 3)

    ,

    ,

    back_inserter(), front_inserter() inserter() : template

    inline

    back_insert_iterator

    back_inserter(Container &c) {

    return back_insert_iterator(c);

    }

    : copy(list1.begin(), list1.end(),

    back_inserter(vector1)); // back_insert_iterator< vector >(vector1));

  • (3 / 3)

    :

    .

    (front_insert_iterator)

    (back_insert_iterator)

    (insert_iterator)

  • (1 / 2)

    STL

    -.

    :

    istream_iterator ;

    ostream_iterator .

    :

    istream_iterator(std::istream&)

    T ;

    istream_iterator()

    (. EOS, end-of-stream);

    ostream_iterator(std::ostream&, char *)

    T

    .

  • (2 / 2)

    :

    merge(

    vector1.begin(), vector1.end(),

    istream_iterator(cin),//

    istream_iterator(), // EOS

    back_inserter(list1));

  • :

    STL

    .

  • (1 / 3)

    STL :

    , ;

    ;

    ( )

    .

  • (2 / 3)

    ,

    , :

    T,

    ;

    binary_function

    () ;

    unary_function

    () ;

    : binary_function

  • (3 / 3)

  • :

    -

    , ,

    ..

  • (1 / 2)

    find find_if

    , ( )

    adjacent_find ,

    count

    count_if

    ,

  • (2 / 2)

    for_each ( )

    mismatch,

    equal

    ( , -)

    search #1 ( ) , ( , -) #2 ( )

    2

  • :

    -, ,

    , , , .

  • (1 / 4)

    copy copy_backward

    ( , )

    fill

    fill_n

    generate ,

    partition

    stable_partition

    -

  • (2 / 4)

    random_shuffle

    remove

    remove_copy

    , , ( )

  • (3 / 4)

    replace

    replace_copy

    ,

    rotate

    swap 1

    swap_ranges

  • (4 / 4)

    transform () () ( )

    unique

  • :

    STL,

    .

  • (1 / 2)

    STL , . , , ( , !), : , , :

    ; ,

    : = .

    , : < C++; - operator

  • (2 / 2)

    STL , , , , , = , ( ). , , .

    : , ==.

    : .

  • , , ,

    , STL.

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

    {

    return y < x;

    }

    greater().

  • sort ( quicksort) log

    2

    partial_sort ( heapsort; )

    log log

    stable_sort ( mergesort; , 2 )

    log log 2 (

    )

  • (1 / 3)

    nth_element - , , ,

    2 (

    )

    binary_search

    log

    lower_bound

    upper_bound

    () ,

    log

  • (2 / 3)

    equal_range ,

    log

    merge ( )

    inplace_merge ( )

    log (

    )

  • (3 / 3)

    min

    max

    ()

    1

    min_element

    max_element

    ()

    lexicographical

    _compare

    ( ) , < ()

    prev_permutation

    next_permutation

    () , < ()

  • :

    STL

    -

    ,

    STL.

  • includes :

    set_union :

    set_intersection :

    set_difference :

    set_symmetric

    _difference

    : =

  • :

    (. heap) ; , : , ,

    (pop), log (push);

    push pop .

  • make_heap ;

    sort_heap ;

    log

    push_heap ; 1 ; ()

    log

    pop_heap ; ; 1 ()

    log

  • :

    ,

    ,

    .

  • accumulate 0, 1, 2, , 1 + 1

    =0

    0 1 1

    partial_sum 0, 1, 2, , 1 0, 0 + 1, 0 + 1 + 2, , 1

    =0

    adjacent

    _difference

    0, 1, 2, , 1 1 0, 2 1, , 1 2

    inner

    _product

    0, 1, 2, , 1 0, 1, 2, , 1

    1

    =0

    0 0 1 1

  • :

    STL:

    ,

    , ;

    :

    .

  • :

    ;

    ;

    ;

    .

    STL :

    template<

    typename T, //

    typename Allocator = allocator >

  • :

    :

    iterator; const_iterator; reverse_iterator; const_reverse_iterator.

    :

    value_type (T); pointer (T*); const_pointer ; reference (T&); const_reference ; difference_type

    ; size_type .

  • :

    STL :

    // O(1)

    vector vector1;

    // O(N), T::T(T&)

    vector vector2(N, value);

    vector vector3(N); // T::T()

    // O(N)

    vector vector4(vector3);

    vector vector5(first, last);

  • : ()

    push_back . 1

    insert

    reserve min ( )

    pop_back 1

    erase

    operator=

    assign

    swap

    1

  • : ()

    begin

    rbegin

    1

    end

    rend

    1

    size 1

    capacity 1

    empty 1

    front

    back

    ()

    1

    operator[N]

    at

    - (at out_of_range)

    1

  • :

    ; ;

    ;

    . :

    template<

    typename T, //

    typename Allocator = allocator >

    vector.

  • : ()

    push_back 1

    push_front 1

    insert

    pop_back 1

    pop_front 1

    erase

    operator=

    assign

    swap

    1

  • : ()

    begin

    rbegin

    1

    end

    rend

    1

    size 1

    empty 1

    front

    back

    ()

    1

    operator[N]

    at

    - (at out_of_range)

    1

  • :

    ; ; ;

    . :

    template<

    typename T, //

    typename Allocator = allocator >

    vector.

  • : ()

    push_back 1

    push_front 1

    insert 1

    pop_back 1

    pop_front 1

    erase

    1

    operator=

    assign

    swap

    1

    splice 1

  • : ( )

    sort sort

    unique unique

    merge merge

    reverse reverse

    remove

    remove_if

    remove,

  • : ()

    begin

    rbegin

    1

    end

    rend

    1

    size 1

    empty 1

    front

    back

    ()

    1

  • :

    ,

    ;

    ;

    .

    STL

    :

    template&l