STL - PowerPoint PPT Presentation

About This Presentation
Title:

STL

Description:

containers can be of several types. sequential (vector, deque, list) associate (set, multiset, map, multimap) adaptive (stack, queue, priority_queue) strings etc ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 19
Provided by: kgan
Learn more at: https://www.cse.fau.edu
Category:
Tags: stl | multimap

less

Transcript and Presenter's Notes

Title: STL


1
STL
  • ANSI and ISO proposed a library of classes and
    functions based on templates. Standard Template
    Library is the result of this standardization.
  • STL supports
  • containers
  • iterators
  • algorithms
  • containers can be of several types
  • sequential (vector, deque, list)
  • associate (set, multiset, map, multimap)
  • adaptive (stack, queue, priority_queue)
  • strings etc

2
The stack adapter
  • include ltstackgt
  • stackltItemTypegt myStack
  • Operations
  • void push(const ItemType item)
  • // push item onto stack
  • void pop() // pop top item from stack
  • ItemType top() // return item on top of stack
  • bool empty() // return true iff stack is empty
  • unsigned size()
  • // return number of elements on stack

3
STL iterators
  • Iterators allow traversal of many containers
  • Using a standard paradigm
  • iterator is subclass of container class
  • Behave much like pointers
  • Many kinds of iterators
  • iterator // allows modification of element
  • const_iterator // elements cannot be changed

4
Itrerator Behavior
  • Common to all iterator types
  • i Advance one element and return a reference
    to i
  • i Advance one element and return the prev
    value of I
  • i Return a reference to the element at i's
    current position.
  • ij Return true if i and j are both positioned
    at the same element.
  • i!j Return true if i and j are positioned at
    different elements.
  • i j Set i's position to the same as j's.
  • Bidirectional
  • --i Retreat one element and return i's new
    value.
  • i-- Retreat one element and return i's previous
    value.

5
Containers Iterators
  • Most collections define the various types of
    iterators they support using typedefs. For
    example, the following are some standard types of
    iterators.
  • iterator Type that can iterate forward through a
    writeable collection
  • const_iterator Type that can iterate forward
    through a read-only collection
  • reverse_iterator Type that can iterate backward
    through a writeable collection
  • const_reverse_iterator Type that can iterate
    backward through a read-only collection
  • Example vectorlt int gtiterator iter1
  • Example listlt int gtreverse_iterator iter2

6
Standard Iterator Functions
  • Most containers provide the following set of
    functions for obtaining an iterator positioned at
    an extremity.
  • iterator begin() Returns an iterator positioned
    at the first item in the collection.
  • const_iterator begin() const Returns a const
    iterator positioned at the first item in the
    collection.
  • iterator end() Returns an iterator positioned
    immediately after the last item in the
    collection.
  • const_iterator end() const Returns a const
    iterator positioned immediately after the last
    item in the collection.
  • For reverse_iterators, you can use rbegin and
    rend.

7
An example of using iterators
  • int main()
  • int data6 -1, 3, 5, 9, 6, 4
  • vectorltintgt a(data, data6)
  • int sum 0
  • vectorltintgtconst_iterator iter
  • for (iter a.begin()
  • iter ! a.end()
  • iter)
  • sum iter
  • cout ltlt Sum is ltlt sum ltlt endl
  • return(0)

8
Iterator Implementation
  • Let us consider the list class we built earlier
    using a linked-list data structure.
  • The problem with the iterators First, Next,
    Current, Done is that you can have only one
    iteration outstanding at any time. So, you cannot
    do nested loop on iterator for example.
  • The iterators we implemented made use of just one
    pointer. It points to the current item.
  • To use these iterators on a const object, we need
    to declare this pointer as mutable or else we
    cant make First, Next etc as const functions.
  • STL style iterators provide a clean approach to
    these issues.

9
Forward Iterator for List Class
  • We need doubly linked list to provide
    bi-directional iterator. So, we stick with
    Forward iterator.
  • We want to be able to insert at current position
    too. i.e insert a new item before the current
    item. Thus, we need to keep track of previous
    pointers. It is convenient to use method 2
    (discussed earlier) for prev.
  • The List class should provide begin and end
    functions. The return type of begin should be
    assignable to a variable of iterator type. So, it
    cant be simply a pointer. It should be a class
    object. end returns similar object.

10
Forward Iterator for List Class (contd)
  • We will create a new class called iterator as a
    subclass of List class.
  • The iterator class should provide , , , !
    e.t.c. operators and copy constructor so that we
    can perform various operations on the iterator.
  • ListltintgtIterator iter a.begin()
  • for (iter a.begin() iter ! a.end()
    iter)
  • If the list class is changed during the use of
    iterators, the next access through the iterator
    may cause problem depending on the situation. We
    will not provide any safety in our first version.
    STL does not provide this kind of safety either.

11
List Class Iterator
  • class List
  • public
  • class Iterator
  • public
  • Iterator(const ListNodePtr ptr NULL)
  • Iterator(const Iterator copyIter)
  • Iterator operator(const Iterator rhs)
  • bool operator(const Iterator rhs) const
  • bool operator!(const Iterator rhs) const

12
List Iterator (contd)
  • ListItem operator() const
  • Iterator operator() // prefix
  • Iterator operator(int) // postfix
  • private
  • ListNodePtr iterPtr
  • Iterator begin() const
  • Iterator end() const
  • ListIteratorIterator(const ListListNodePtr
    ptr NULL)
  • iterPtr(ptr)

13
List Iterator (contd)
  • ListIteratorIterator(const ListIterator
    copyIter) iterPtr(copyIter.iterPtr)
  • bool ListIteratoroperator
  • (const ListIterator rhs)
  • return (iterPtr rhs.iterPtr)

14
List Iterator (contd)
  • ListIterator ListIteratoroperator
  • (const ListIterator rhs)
  • if (this rhs)
  • return (this)
  • // There is nothing to destroy.
  • // Simply copy the pointer.
  • iterPtr rhs.iterPtr
  • return(this)

15
List Iterator (contd)
  • bool ListIteratoroperator!(
  • const ListIterator rhs)
  • return(iterPtr ! rhs.iterPtr)
  • ListIterator ListIteratoroperator()
  • if (iterPtr)
  • iterPtr iterPtr-gtnext
  • return(this)

16
List Iterator (contd)
  • ListIterator ListIteratoroperator(int)
  • ListIterator temp this
  • if (iterPtr)
  • iterPtr iterPtr-gtnext
  • return(temp)

17
List Iterator (contd)
  • ListItem ListIteratoroperator()
  • ListItem junk
  • // uses default constructor
  • if (iterPtr)
  • return(iterPtr-gtitem)
  • return(junk)

18
List Iterator (contd)
  • Iterator Listbegin() const
  • return (Iterator(head))
  • Iterator Listend() const
  • return (Iterator())
Write a Comment
User Comments (0)
About PowerShow.com