Standard Template Library - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Standard Template Library

Description:

Iterator is an object that can navigate over elements. ... Again multimap allows duplication but map not. They are binary balanced trees ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 46
Provided by: sfu5
Category:

less

Transcript and Presenter's Notes

Title: Standard Template Library


1
Standard Template Library
  • Alireza Fathi

2
STL
  • Containers
  • Iterators
  • Algorithms
  • An open-closed environment
  • Open for extension and closed for modification

3
containers
  • Sequence containers
  • Ordered collections
  • Sorted by the sequence of addition
  • Associative containers
  • Sorted collections
  • Sorted by value (key)
  • Container adapters

4
Sequence containers
  • Vectors
  • Deques
  • Lists

5
Associative containers
  • Sets
  • Multisets
  • Maps
  • Multimaps

6
Container adapters
  • Stacks
  • Queues
  • Priority queues

7
Iterators
  • Iterator is an object that can navigate over
    elements.
  • An iterator points to a certain position of a
    container.
  • Similar to pointers

8
Algorithms
  • There are several algorithms in STL such as
    searching, sorting, copying, reordering .

9
Common container operations
  • Some constructors, destructor
  • , ! , lt , gt , lt , gt ,
  • c.size( )
  • c.empty( )
  • swap(c1,c2)
  • c.begin( ) , c.rbegin( )
  • c.end( ) , c.rend( )
  • c.insert(pos,element)
  • c.earase(beg,end)
  • c.clear( )
  • c.max_size( )

10
Vector
  • A vector models a dynamic array
  • The elements always have a certain order
  • Vectors provide random access
  • Good performance for appending or deleting at the
    end but not middle
  • Pointers are interpreted by insertion in middle

11
Vector operations
  • C.capacity( )
  • C.reserve(int)
  • C.at(idx)
  • Cidx
  • C.front( )
  • C.back( )
  • C.insert(pos,elem) C.insert(pos,n,elem)
  • C.push_back(elem)
  • C.pop_back(elem)
  • C.earase(pos) C.earase(beg,end)
  • C.resize(num) C.resize(num,elem)
  • C.clear( )

12
Deques
  • Very similar to vector.
  • Dynamic array, random access, same interface
  • But dynamic array in deque open at both ends

13
Deques compared with Vectors
  • Fast insertion and removing at both beginning and
    the end
  • A bit slower than vectors
  • Iterators are smart pointers of a special type
    because they can jump between blocks
  • For systems have limited block sizes of memory
    deques are larger
  • We can not avoid reference interpretation in
    deques
  • The container releases the memory if it is no
    farther used

14
Extended deque operations
  • C.push_back(elem)
  • C.push_front(elem)
  • C.pop_back(elem)
  • C.pop_front(elem)
  • C.front( )
  • C.end( )

15
Lists
  • A list manages its elements as a doubly linked
    list
  • The list is different in several ways with a
    vector or deque

16
Abilities of lists
  • Does not provide random access
  • Inserting and removing elements is fast at each
    positions and not just the ends
  • Inserting and deleting pointers does not
    invalidate pointers, references, and iterators or
    other elements
  • A list supports exception handling in a way that
    every operation succeeds or is no-op.

17
Lists compared with vec deq
  • There is no at( ) operation in lists
  • Dont provide operation for capacity
  • Lists provide many special member functions for
    moving elements.
  • Function c1.splice(pos,c2,c2pos)
  • c1.merge(c2) c1.merge(c2,op)
  • c1.sort( ) c1.sort(op)
  • c1.unique( ) c1.unique(op)
  • c1.reverse( )

18
Sets and Multisets
  • Sets and Multisets sort their elements
    automatically according to a certain sorting
    criterion.
  • The difference between two is the multisets allow
    duplicates but sets not.
  • Sets and Multisets do not provide operations for
    direct part access
  • Red-black trees
  • Red-black trees are good for both changing the
    number of elements and searching the tree

19
Search operations
  • count(elem)
  • find(elem)
  • lower_bound(elem)
  • The first position elem will inserted
  • upper_bound(elem)
  • The last position elem will inserted
  • equal_range(elem)
  • The range of elements elem

20
Maps and Multimaps
  • Are containers that manage key/value pairs as
    elements.
  • Sorting occurs due to key.
  • Again multimap allows duplication but map not.
  • They are binary balanced trees
  • Very similar to sets

21
  • Hash tables are not included in C standard
    because they say .
  • But you can use some implementations of it
  • hash_set, hash_multiset, hash_map, hash_multimap
  • I can refer you to The C programming language
    by stroustrup 17.6

22
Stack
  • push( )
  • pop( )
  • top( )

23
Queue
  • push( )
  • pop( )
  • front( )
  • back( )

24
Priority_queue
  • push( )
  • pop( )
  • top( )

25
Iterators
  • Header files for iterators
  • Special iterators ltiteratorgt
  • Like pointers

26
  • input iterator output iterator
  • forward iterator
  • bidirectional iterator
  • random access iterator

27
  • Input iterator reads forward
  • Output iterator writes forward
  • Forward iterator reads and writes forward
  • Bidirectional iterator reads and writes forward
    and backward
  • Random access iterator reads and writes with
    random access

28
Operations for Input iterators
  • iter
  • iter -gt member
  • iter
  • iter
  • iter1 iter2
  • iter1 ! iter2
  • You can read every thing just once and you can
    just read

29
Operations for output iterators
  • iter value
  • iter
  • iter
  • You can write every thing just once and you can
    just write and nothing else

30
Operation of forward iterators
  • iter
  • iter-gtmember
  • iter
  • iter
  • iter1 iter2
  • iter1 ! iter2
  • iter1 iter2

31
Operations for bidirectional iterators
  • --iter
  • iter--
  • Plus all operations of forward iterator

32
Operations of random access iterator
  • All operations of bidirectional iterators
  • itern
  • iter n
  • iter - n
  • itern
  • niter
  • iter-n
  • iter1 iter2
  • iter1 lt iter2
  • iter1 gt iter2
  • iter1 lt iter2
  • iter1 gt iter2

33
  • vectorltintgt coll
  • vectorltintgtiterator pos
  • pos coll.begin( )

34
  • include ltiteratorgt
  • void advance (InputIterator pos, Dist n)
  • Dist distance (InputIterator pos1, InputIterator
    pos2)
  • void Iter_swap (ForwardIterator pos1,
    ForwardIterator pos2)
  • Swaps the value to which iterators pos1 pos2
    are reffered

35
Algorithms
  • include ltalgorithmgt
  • The _if suffix
  • Passing either a value or a function
  • find( ) find_if( )
  • The _copy suffix
  • Not only manipulated but also copied
  • reverse( ) reverse_copy( )

36
Classifying algorithms
  • Nonmodifying algorithms
  • Neither change the value nor the order of
    elements
  • Modifying algorithms
  • Change the value of elements
  • Removing algorithms
  • A special case of modifying algorithms that can
    remove elements
  • Mutating algorithms
  • They change the order of elements (and not their
    values) by assigning and swapping their values
  • Sorting algorithms
  • Are also special kind of mutating algorithms
    because change the order
  • Sorted range algorithms
  • They require that the ranges on which they
    operate are sorted according to their sorting
    criterion
  • Numeric algorithms
  • These algorithms combine numeric elements in
    different ways

37
Nonmodifyings
  • for_each( )
  • count( )
  • count_if( )
  • min_element( )
  • max_element( )
  • find( )
  • find_if( )
  • search_n( )
  • search( )
  • find_end( )
  • find_first_of( )
  • adjacent_of( )
  • equal( )
  • mismatch( )
  • lexicographical_compare( )

38
Modifyings
  • for_each( )
  • copy( )
  • copy_backward( )
  • transform( )
  • merge( )
  • swap_ranges( )
  • fill( )
  • fill_n( )
  • generate( )
  • generate_n( )
  • replace( )
  • replace_if( )
  • replace_copy( )
  • replace_copy_if( )

39
Removings
  • remove( )
  • remove_if( )
  • remove_copy( )
  • remove_copy_if( )
  • unique( )
  • unique_copy( )

40
Mutatings
  • reverse( )
  • reverse_copy( )
  • rotate( )
  • rotate_copy( )
  • next_permutation( )
  • prev_permutation( )
  • random_shuffle( )
  • partition( )
  • stable_partition( )

41
Sortings
  • sort( )
  • stable_sort( )
  • partial_sort( )
  • partial_sort_copy( )
  • nth_element( )
  • partition( )
  • stable_partition( )
  • make_heap( )
  • push_heap( )
  • pop_heap( )
  • sort_heap( )

42
Sorted ranges
  • binary_search( )
  • includes( )
  • lower_bound( )
  • upper_bound( )
  • equal_range( )
  • merge( )
  • set_union( )
  • set_intersection( )
  • set_difference( )
  • set_symmetric_difference( )
  • inplace_merge( )

43
Numerics
  • accumulate( )
  • inner_product( )
  • adjacent_difference( )
  • partial_sum( )

44
My references through these days!!!
  • 1 C -programming language Stroustrup
  • 2 C - how to Deitel
  • 3 UML with rational rose Buggs
  • 4 The C standard library Josuttis
  • 5 C FAQs Cline
  • 6 Data structures and program design in C
    Kruse
  • 7 A complete guide to programming in C
    Kirch
  • 8 Visual C 6.0 Microsoft
  • 9 Networking a beginners guide Hallberg
  • 10 - .

45
AYLOOZ
  • Hope you had a happy semester
Write a Comment
User Comments (0)
About PowerShow.com