STL: C Standard Library (continued) - PowerPoint PPT Presentation

About This Presentation
Title:

STL: C Standard Library (continued)

Description:

Different types of iterators - to support read, write and ... Bi-directional: supports also decrement. Random: supports random access (just like C pointer) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 32
Provided by: csHu
Category:

less

Transcript and Presenter's Notes

Title: STL: C Standard Library (continued)


1
STL C Standard Library(continued)
2
STL Iterators
  • Iterators are allow to traverse sequences
  • Methods
  • operator
  • operator-gt
  • operator, and operator
  • Different types of iterators - to support read,
    write and random access
  • Containers define their own iterator types
  • Changing the container can invalidate the iterator

3
Iterator Types
  • Output write only and can write only once
  • Input read many times each item
  • Forward supports both read and write
  • Bi-directional supports also decrement
  • Random supports random access (just like C
    pointer)

4
Iterators Containers
  • Bidirectional iterators
  • list, map, set
  • Random access iterators
  • vector, deque
  • Input/output/forward iterators
  • iostreams

5
Iterators Containers
  • Every STL container T provides
  • class T
  • typedef iterator // iterator type T
  • iterator begin() // first element of the
    container
  • iterator end( // element after last of the
    container

6
Iterators Containers
  • Typical code will look like
  • Container C
  • Containeriterator i
  • for( i C.begin() i ! C.end() i)
  • // do something with i

7
Iterators Containers
  • Iterators allow to access/modify elements
  • Container C
  • Containeriterator i,j
  • C.insert(i,x) insert x before i
  • C.insert(i,first,last) insert elements in
    first,last) before i
  • C.erase(i) erase element i points to
  • C.erase(i,j) erase elements in range i,j)

8
Iterator validity
  • When working with iterators, we have to remember
    that their validity can change
  • Whats wrong with this code?
  • Container C
  • Citerator i
  • for( i C.begin() i ! C.end() i )
  • if( f( i ) ) // some test
  • C.erase(i) // remove this element

9
Iterator validity
  • Two cases
  • list, set, map
  • i is not a legal iterator
  • vector
  • i points to the element after
  • In either case, this is not what we want

10
Iterator validity
  • Second try
  • Container C
  • Citerator i C.begin()
  • while( i ! C.end() )
  • Citerator j i
  • if( f( j ) ) // some test
  • C.erase(j) // remove this element
  • Works for set, map, list, not vector or deque

11
Iterators and Map
  • Suppose we work with
  • mapltstring,intgt dictionary
  • mapltstring,intgtiterator i
  • i dictionary.begin()
  • What is the type of i ?

12
Iterators and Map
  • Every STL container type Container defines
  • Containervalue_type
  • Type of elements stored in container
  • This is the type returned by an iterator
  • Containervalue_type
  • Containeriterator operator()

13
Iterators and Map
  • Ok, so what type of elements does a map return?
  • Recall
  • mapltKeyType,ValueTypegt keeps pairs
  • KeyType key key of entry
  • ValueType value value of entry

14
Pairs
  • templatelt class T1, class T2gt
  • struct pair
  • typedef T1 first_type
  • typedef T2 first_type
  • T1 first
  • T2 second
  • pair( const T1 x, const T2 y )
  • first(x), second(y)

15
Map value_type
  • templatelt class Key, class T,
  • class Cmp lessltKeygt gt
  • class map
  • public
  • typedef pairltconst Key, Tgt value_type
  • typedef Key key_type
  • typedef T mapped_type
  • typedef Cmp key_compare

16
Using map iterator
  • mapltstring,intgt dict
  • mapltstring,intgtiterator i
  • for( i dict.begin()
  • i ! dict.end()
  • i )
  • cout ltlt i-gtfirst ltlt
  • ltlt i-gtsecond ltlt \n
  • See dictionary.cpp

17
Iterators and Assoc. Containers
  • Additional set of operations
  • iterator Cfind(key_type const key)Return
    iterator to first element with key. Return end()
    if not found
  • iterator Clower_bound(key_type const
    key)Return iterator to first element greater or
    equal to key
  • iterator Cupper_bound(key_type const
    key)Return iterator to first element greater
    than key

18
Iterators Streams
  • Can access iostreams through iterators
  • istream_iteratorltstringgt in(cin)
  • istream_iteratorltstringgt endOfInput
  • ostream_iteratorltstringgt out(cout)
  • while( in ! endOfInput )
  • string s in
  • out s
  • out
  • see useStreamIterators.cpp

19
Inserters
  • istream_iteratorltstringgt in(cin)
  • istream_iteratorltstringgt endOfInput
    vectorltstringgt vect
  • back_insert_iteratorltvectorltstringgt gt
  • back(vect)
  • // copy input words into vector
  • while( in ! endOfInput )
  • back in
  • // same as vect.push_back(in)

20
Inserters
  • Inserters are output iterators
  • back_insert_iteratorltCgt( C c)
  • Insert at back of c
  • front_insert_iteratorltCgt( C c)
  • Insert at front of c
  • insert_iteratorltC,Itergt(C c, Iter i)
  • Insert at just before i
  • Allow to write into containers in generic
    algorithms

21
Do-it-yourself iterators
  • You can create iterators
  • Check list
  • Define the appropriate operators
  • Ensure copy constructor/operator
  • Define the right typedefsuse inheritance from
    iteratorlt..gt class
  • See TokenIterator.h

22
Sequence Adapters
  • Adapters of basic containers
  • Very easy, but limited interface
  • stackltT,Seqgt
  • provides push, pop, top, size and empty
  • queueltT,Seqgt
  • also provides back
  • priority_queueltT,Seq,Cmpgt
  • provides same interface as stack
  • uses a compare function object

23
Container summary
List ops Front ops Back ops Iterators
vector const n const Random
list const const const Bi-direct
deque const n const const Random
stack const
queue const const
priority_queue log(n) log(n)
map log(n) log(n) Bi-direct
set log(n) Bi-direct
24
Algorithms Overview
  • Sequence operations
  • Non modifying for_each, find, count, search,
    mismatch, equal
  • Modifying transform, copy, swap, replace, fill,
    generate, remove, unique, reverse, rotate,
    random_shuffle
  • Sorted sequences operations
  • sort, lower_bound, upper_bound, equal_range,
    binary_search, merge, includes, set_union,
    intersection, difference, symmetric_difference

25
Algorithms
  • Most STL algorithms works on sequences
  • Sequences are passed as two iterators
  • beginning element
  • element one after last
  • Algorithms depend on iterator type
  • not on container type

26
Copy
  • templatelt class In, class Outgt
  • Out copy(In first, In last, Out res)
  • while (first ! last)
  • res first
  • return res
  • See useCopy.cpp

27
Non-modifying Sequence Algorithms
  • In find(In first, In last, const T val)
  • find the first occurence of val in the sequence
  • In find_if(In first, In last, Pred p) find the
    first element satisfying p
  • I1 find_first_of(I1 f1, I2 l1, I2 f2, I2 l2)
    find the first match between two sequences.
  • I1 search( I1 f1, I1 l1, I2 f1, I2 l2 )search
    for the sequence f2...l2 inside f1..l1

28
Sorted Sequence Algorithms
  • sort(In first, In last, class cmp)
  • find the first occurence of val in the sequence
  • In lower_bound(In first, In last,
    T const val, class cmp) find the first
    element not less than val
  • bool binary_search(In first, In last,
    T const val, class cmp) check if val
    appears in the sequence
  • Out merge( I1 f1, I1 l1, I2 f1, I2 l2,
    Out out )merge two sorted sequences and write
    the merged sequence onto the output iterator out

29
Ex4 Interactive Graph Operations
  • An interactive shell that allows to perform graph
    operations
  • Gcalcgt G1 x11, x22 ltx11, x22gt Gcalcgt G2
    x11, x22 ltx11, x22gt
  • Gcalcgt G3 G1G2
  • Gcalcgt print G3

30
Ex5 Preview
  • Extend the graph calculator
  • Weighted graphs
  • Algorithms shortest path, minimum spanning tree,
    max flow, etc.
  • Load/Save file

31
Ex4 Components
  • Graph data structure
  • Symbol Table
  • Command parser
  • Command evaluater
  • Emphasis extendibility of command syntax for
    next exercise

32
Graph Data Structure
  • Your design show allow to
  • Constructors/copy etc.
  • Add vertices/edges
  • Iterate over vertices
  • Iterate over parents of a vertex
  • Iterate over children of a vertex
  • Graph operations
Write a Comment
User Comments (0)
About PowerShow.com