the stl
TRANSCRIPT
Introduction Iterators Function Objects Summary References
THE STL
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 22, 2015
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
OUTLINE I
INTRODUCTION
ITERATORS
FUNCTION OBJECTS
SUMMARY
REFERENCES
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
INTRODUCTION
I The STL is a rich collection of standard data structures,recently added to C++.
I Will elevate data structures to the level of, say, the I/Ostream package (i.e., something taken for granted).
I In many ways, is an anti-object-oriented design, yet it ispowerful and it works.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
GENERIC ALGORITHMS VS ENCAPSULATION
I Object-Oriented programming holds encapsulation as anideal, bundling all actions in one place.
I Can make for “fat” classes.I The STL separates data structures and algorithms.I The data structures are relatively small. The algorithms are
many. Can be mixed and matched.I Allows for a much smaller class library.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
ITERATORS
I Basic problem – how do you allow access to elements ofcollection, without knowing how the collection isorganized?
I Solution, define a new data type specifically for creatingloops.
I A large number of algorithms are provided by the standardlibrary, all built using iterators.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
DESCRIBING A RANGE OF VALUES
I Notice how a range of values is often described by astarting value and a past-the-end value.
I The past the end value is not part of the collection, but justa marker.
FIGURE : A Range of Values
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
BEGIN AND END
I By convention, containers return a starting value inresponse to begin(), and a past-the-end value in responseto end().
I For example, to shuffle a vector of values:
BEGIN AND END
random_shuff le( aVector . begin ( ) , aVector . end ( ) , randomInteger ) ;
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
WHAT MUST ITERATORS DO?I To see what iterators must do, consider a typical algorithm:
EXAMPLE
template <class T>i t e r a t o r f i n d
( i t e r a t o r f i r s t , i t e r a t o r l a s t , T & value ){
while ( f i r s t != l a s t && ∗ f i r s t != value ) ++ f i r s t ;return f i r s t ;
}
Could be used to find values in an array, or in a list:
EXAMPLE
i n t data [ 1 0 0 ] ;. . .
i n t ∗ where = f i n d ( data , data +100 , 7 ) ;l i s t < int > : : i t e r a t o r where = f i n d ( a L i s t . begin ( ) , a L i s t . end ( ) , 7 ) ;
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
ITERATORS OPERATIONS
I An iterator can be compared for equality to another iterator.I They are equal when they point to the same position, and
are otherwise not equal.I An iterator can be dereferenced using the * operator, to
obtain the value being denoted by the iterator.I Depending upon the type of iterator and variety of
underlying container, this value can also sometimes beused as the target of an assignment in order to change thevalue being held by the container.
I An iterator can be incremented, so that it refers to the nextelement in sequence, using the operator ++.
I What makes iterators possible is that all of these can beoverloaded.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
FUNCTION OBJECTS
I In C++ even the function call operator (parenthesisoperator) can be overloaded.
I Allows for creation of objects that can be used likefunctions.
EXAMPLE
class biggerThanThree {public :
bool operator ( ) ( i n t v ){ return v > 3; }
} ;
Can be used to find the first value bigger than 3, for example.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
WHY DO THIS?
Objects can take arguments computed at run-time, specializefunctions in a way that simple functions cannot:
EXAMPLE
class biggerThan {public :
biggerThan ( i n t x ) : tes tVa lue ( x ) { }const i n t tes tVa lue ;bool operator ( ) ( i n t va l )
{ return va l > tes tVa lue ; }} ;
l i s t < int > : : i t e r a t o r f i r s t B i g =f i n d _ i f ( a L i s t . begin ( ) , a L i s t . end ( ) , biggerThan ( 1 2 ) ) ;
In functional languages, this kind of object is sometimes knownas a curry.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
EXAMPLE PROGRAM – INVENTORY SYSTEM
A business, WorldWideWidgetWorks, manufactures widgets.
EXAMPLE
class Widget {public :
/ / cons t ruc to rsWidget ( ) : id_number ( 0 ) { }Widget ( i n t a ) : i d ( a ) { }
/ / opera t ionsi n t i d ( ) { return id_number ; }void operator = ( Widget & rhs )
{ id_number = rhs . id_number ; }bool operator == ( Widget & rhs )
{ id_number == rhs . id_number ; }bool operator < ( Widget & rhs )
{ id_number < rhs . id_number ; }protected :
i n t id_number ; / / widget i d e n t i f i c a t i o n number} ;
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
INVENTORY
I Inventory is two lists - items on hand, items on back order.
EXAMPLE
class i n ven to ry {public :
/ / process order f o r widget type widvoid order ( i n t wid ) ;
/ / rece ive widget o f type wid/ / i n shipment
void rece ive ( i n t wid ) ;
protected :l i s t <Widget> on_hand ;l i s t < int > on_order ;
} ;
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
ITEM ARRIVES IN SHIPMENT
EXAMPLE
void i n ven to ry : : rece ive ( i n t wid )/ / process a widget rece ived i n shipment
{ cout << " Received shipment o f widget type " << wid << endl ;
l i s t < int > : : i t e r a t o r we_need =f i n d ( on_order . begin ( ) , on_order . end ( ) , wid ) ;
i f ( we_need != on_order . end ( ) ) {cout << " Ship " << Widget ( wid ) << " to f i l l back order " << endl ;on_order . erase ( we_need ) ;}
elseon_hand . push_f ront ( Widget ( wid ) ) ;
}
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
ITEM ARRIVES ON ORDER
EXAMPLE
void i n ven to ry : : order ( i n t wid )/ / process an order f o r a widget w i th given i d number
{ cout << " Received order f o r widget type " << wid << endl ;
l i s t <Widget > : : i t e r a t o r we_have =f i n d _ i f ( on_hand . begin ( ) , on_hand . end ( ) , WidgetTester ( wid ) ) ;
i f ( we_have != on_hand . end ( ) ) {cout << " Ship " << ∗wehave << endl ;on_hand . erase ( we_have ) ;}
else {cout << " Back order widget o f type " << wid << endl ;on_order . push_f ront ( wid ) ;}
}
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
FUNCTION OBJECTS
I Created like an instance of a class, works like a function.
EXAMPLE
class WidgetTester {public :
WidgetTester ( i n t i d ) : t e s t _ i d ( i d ) { }i n t t e s t _ i d ;
/ / de f ine the f u n c t i o n c a l l opera torbool operator ( ) ( Widget & wid )
{ return wid . i d ( ) == t e s t _ i d ; }} ;
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
MAPS, INDEXED DATA STRUCTURES
MAPS
typedef map< s t r i n g , int > s t r i n g V e c t o r ;typedef map< s t r i n g , s t r i ngVec to r > graph ;
s t r i n g pendleton ( " Pendleton " ) ;s t r i n g pensacola ( " Pensacola " ) ;s t r i n g peor ia ( " Peor ia " ) ;s t r i n g phoenix ( " Phoenix " ) ;s t r i n g p i e r r e ( " P ie r re " ) ;s t r i n g p i t t s b u r g h ( " P i t t sbu rgh " ) ;s t r i n g pr ince ton ( " Pr inceton " ) ;s t r i n g pueblo ( " Pueblo " ) ;
graph cityMap ;
ci tyMap [ pendleton ] [ phoenix ] = 4 ;ci tyMap [ pendleton ] [ pueblo ] = 8 ;ci tyMap [ pensacola ] [ phoenix ] = 5 ;ci tyMap [ peor ia ] [ p i t t s b u r g h ] = 5 ;ci tyMap [ peor ia ] [ pueblo ] = 3 ;ci tyMap [ phoenix ] [ peor ia ] = 4 ;ci tyMap [ phoenix ] [ p i t t s b u r g h ] = 10;ci tyMap [ phoenix ] [ pueblo ] = 3 ;ci tyMap [ p i e r r e ] [ pendleton ] = 2 ;ci tyMap [ p i t t s b u r g h ] [ pensacola ] = 4 ;ci tyMap [ p r ince ton ] [ p i t t s b u r g h ] = 2 ;ci tyMap [ pueblo ] [ p i e r r e ] = 3 ;The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
THE GRAPH BEING REPRESENTED
FIGURE : Graph.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
REPRESENTING A DISTANCE
EXAMPLE
struct Dis tancePai r {unsigned i n t f i r s t ;s t r i n g second ;Dis tancePai r ( ) : f i r s t ( 0 ) { }D is tancePai r ( unsigned i n t f , s t r i n g & s )
: f i r s t ( f ) , second ( s ) { }} ;
bool operator < ( Dis tancePai r & lhs , D is tancePai r & rhs ){ return l hs . f i r s t < rhs . f i r s t ; }
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
SHORTEST PATH ALGORITHM
EXAMPLE
void shor tes tD is tance ( const graph & cityMap ,const s t r i n g & s t a r t , s t r i n g V e c t o r & d is tances )
{/ / process a p r i o r i t y queue of d is tances to c i t i e spr io r i t y_queue <DistancePair , vector <DistancePair > ,
greater <DistancePai r > > que ;que . push ( Dis tancePai r (0 , s t a r t ) ) ;
while ( ! que . empty ( ) ) {/ / p u l l nearest c i t y from queue
i n t d is tance = que . top ( ) . f i r s t ;s t r i n g c i t y = que . top ( ) . second ;que . pop ( ) ;
/ / i f we haven ’ t seen i t a l ready , process i ti f (0 == d is tances . count ( c i t y ) ) {
/ / then add i t to sho r t es t d is tance mapd is tances [ c i t y ] = d is tance ;
/ / and put values i n t o queues t r i n g V e c t o r : : i t e r a t o r s t a r t , stop ;s t a r t = cityMap [ c i t y ] . begin ( ) ;stop = cityMap [ c i t y ] . end ( ) ;for ( ; s t a r t != stop ; ++ s t a r t )
que . push ( Dis tancePai r ( d is tance + (∗ s t a r t ) . second ,(∗ s t a r t ) . f i r s t ) ) ;
}}
}The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
SUMMARY
I The Future of OOP.I Given that the STL is not OO, does this mean that OOP is
dead?I No, only that inspiration can come from a number of
sources, and not all problems are best solved in an OOPfashion.
I However, many problems ARE best approached in anOOP manner.
I Bottom line - know as much as you can about as manystyles of programming as you can, and use the style mostappropriate to the problem.
The STL Roaming Researchers, Inc. cbna
Introduction Iterators Function Objects Summary References
REFERENCES
I Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.
I An Introduction to Object Oriented Programming, TimothyBudd.
I This presentation is developed with Beamer:I Szeged, beetle.
The STL Roaming Researchers, Inc. cbna