Computer Notes - Vector - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Computer Notes - Vector

Description:

- Computer Notes - Vector in Object oriented Programming what is Vector Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:172
Slides: 32
Provided by: ecomputernotes
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Computer Notes - Vector


1
Vector
http//ecomputernotes.com
2
Recap
  • Generic algorithm requires three operations (,
    , !)
  • Implementation of these operations in Vector
    class
  • Problems
  • No support for multiple traversals
  • Supports only a single traversal strategy
  • Inconsistent behavior
  • Operator !

http//ecomputernotes.com
3
Cursors
  • A better way is to use cursors
  • A cursor is a pointer that is declared outside
    the container / aggregate object
  • Aggregate object provides methods that help a
    cursor to traverse the elements
  • T first()
  • T beyond()
  • T next( T )

http//ecomputernotes.com
4
Vector
  • templatelt class T gt
  • class Vector
  • private
  • T ptr
  • int size
  • public
  • Vector( int 10 )
  • Vector( const Vectorlt T gt )
  • Vector()
  • int getSize() const

http//ecomputernotes.com
5
Vector
  • const Vectorlt T gt operator ( const
    Vectorlt T gt )
  • T operator ( int )
  • T first()
  • T beyond()
  • T next( T )

http//ecomputernotes.com
6
Vector
  • templatelt class T gt
  • T Vectorlt T gtfirst()
  • return ptr
  • templatelt class T gt
  • T Vectorlt T gtbeyond()
  • return ( ptr size )

http//ecomputernotes.com
7
Vector
  • templatelt class T gt
  • T Vectorlt T gtnext( T current )
  • if ( current lt (ptr size) )
  • return ( current 1 )
  • // else
  • return current

http//ecomputernotes.com
8
Example Cursor
  • int main()
  • Vectorlt int gt iv( 3 )
  • iv0 10
  • iv1 20
  • iv2 30
  • int first iv.first()
  • int beyond iv.beyond()
  • int found find(first,beyond,20)
  • return 0

http//ecomputernotes.com
9
Generic Algorithm
  • templatelt typename P, typename T gt
  • P find( P start, P beyond,
  • const T x )
  • while ( start ! beyond
  • start ! x )
  • start
  • return start

http//ecomputernotes.com
10
Cursors
  • This technique works fine for a contiguous
    sequence such as Vector
  • However it does now work with containers that use
    complicated data structures
  • There we have to rely on the container traversal
    operations

http//ecomputernotes.com
11
Example Works Fine
a
b
c
d
g
f
e

Cursor
http//ecomputernotes.com
12
Example Problem
a
b
d
c

Cursor
http//ecomputernotes.com
13
Example Problem
  • int main()
  • Setlt int gt is( 3 )
  • is.add( 10 )
  • is.add( 20 )
  • is.add( 30 )
  • ET first iv.first()
  • ET beyond iv.beyond()
  • ET found find(first, beyond, 20)
  • return 0

http//ecomputernotes.com
14
Example Problem
  • templatelt typename P, typename T gt
  • P find( P start, P beyond,
  • const T x )
  • while ( start ! beyond
  • start ! x )
  • start // Error
  • return start

http//ecomputernotes.com
15
Works Fine
  • templatelt typename CT, typename ET gt
  • P find( CT cont, const ET x )
  • ET start cont.first()
  • ET beyond cont.beyond()
  • while ( start ! beyond
  • start ! x )
  • start cont.next( start )
  • return start

16
Works Fine
  • int main()
  • Setlt int gt is( 3 )
  • is.add( 10 )
  • is.add( 20 )
  • is.add( 30 )
  • int found find( is, 20 )
  • return 0

http//ecomputernotes.com
17
Cursors Conclusion
  • Now we can have more than one traversal pending
    on the aggregate object

a
b
c
d
g
f
e

Cursor 2
Cursor 1
18
Cursors Conclusion
  • However we are unable to use cursors in place of
    pointers for all containers

http//ecomputernotes.com
19
Iterators
  • Iterator is an object that traverses a container
    without exposing its internal representation
  • Iterators are for containers exactly like
    pointers are for ordinary data structures

20
Generic Iterators
  • A generic iterator works with any kind of
    container
  • To do so a generic iterator requires its
    container to provide three operations
  • T first()
  • T beyond()
  • T next( T )

http//ecomputernotes.com
21
Example Generic Iterator
Container
Iterator
first() beyond() next()
operator operator
22
Generic Iterator
  • templatelt class CT, class ET gt
  • class Iterator
  • CT container
  • ET index
  • public
  • Iterator( CT c,
  • bool pointAtFirst true )
  • Iterator( Iteratorlt CT, ET gt it )
  • Iterator operator ()
  • ET operator ()

http//ecomputernotes.com
23
Generic Iterator
  • bool operator !(
  • Iteratorlt CT, ET gt it )

24
Generic Iterator
  • templatelt class CT, class ET gt
  • Iteratorlt CT, ET gtIterator( CT c, bool
    pointAtFirst )
  • container c
  • if ( pointAtFirst )
  • index container-gtfirst()
  • else
  • index container-gtbeyond()

http//ecomputernotes.com
25
Generic Iterator
  • templatelt class CT, class ET gt
  • Iteratorlt CT, ET gtIterator( Iteratorlt CT,
    ET gt it )
  • container it.container
  • index it.index

26
Generic Iterator
  • templatelt class CT, class ET gt
  • IteratorltCT,ETgt IteratorltCT,ETgt operator
    ()
  • index container-gtnext( index )
  • return this

http//ecomputernotes.com
27
Generic Iterator
  • templatelt class CT, class ET gt
  • ET Iteratorlt CT, ET gtoperator ()
  • return index

28
Generic Iterator
  • templatelt class CT, class ET gt
  • bool Iteratorlt CT, ET gtoperator !( Iteratorlt
    CT, ET gt it )
  • if ( container ! it.container index
    ! it.index )
  • return true
  • // else
  • return false

http//ecomputernotes.com
29
Generic Iterator
  • int main()
  • Vectorlt int gt iv( 2 )
  • Iteratorlt Vectorltintgt, int gt
  • it( iv ), beyond( iv, false )
  • iv0 10
  • iv1 20
  • Iteratorlt Vectorltintgt, int gt found
  • find( it, beyond, 20 )
  • return 0

http//ecomputernotes.com
30
Generic Iterator
  • templatelt typename P, typename T gt
  • P find( P start, P beyond,
  • const T x )
  • while ( start ! beyond
  • start ! x )
  • start
  • return start

31
Iterators Conclusion
  • With iterators more than one traversal can be
    pending on a single container
  • Iterators allow to change the traversal strategy
    without changing the aggregate object
  • They contribute towards data abstraction by
    emulating pointers

http//ecomputernotes.com
About PowerShow.com