









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of cursors and iterators in object-oriented programming (oop). A cursor is a pointer declared outside the container that helps traverse its elements using methods like first(), beyond(), and next(). Iterators, on the other hand, are objects that traverse containers without exposing their internal representation. They provide methods like operator * and operator ++. The document also discusses the difference between cursors and iterators and their advantages.
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










(^) T* first()T* first() (^) T* beyond()T* beyond() (^) T* next( T* )T* next( T* )
template< class T > template< class T > T* Vector< T >::next( T* current ) T* Vector< T >::next( T* current )
{ { if ( current < (ptr + size) ) if ( current < (ptr + size) ) return ( current + 1 ); return ( current + 1 ); // else // else return current; return current; } }
template< typename P, typename T > template< typename P, typename T > P find( P start, P beyond, P find( P start, P beyond, const T& x ) { const T& x ) { while ( start != beyond && while ( start != beyond && start != x )start != x ) ++start; ++start;
return start; return start; } }
int main() { int main() {
Set< int > is( 3 ); Set< int > is( 3 ); is.add( 10 ); is.add( 10 ); is.add( 20 ); is.add( 20 ); is.add( 30 ); is.add( 30 ); ET* first = iv.first(); ET* first = iv.first(); ET* beyond = iv.beyond(); ET* beyond = iv.beyond(); ET* found = find(first, beyond, 20); ET* found = find(first, beyond, 20); return 0; return 0;
} } docsity.com
template< typename CT, typename ET > template< typename CT, typename ET > P find( CT& cont, const ET& x ) { P find( CT& cont, const ET& x ) { ET* start = cont.first(); ET* start = cont.first(); ET* beyond = cont.beyond(); ET* beyond = cont.beyond(); while ( start != beyond && while ( start != beyond && start != x )start != x ) start = cont.next( start ); start = cont.next( start ); return start; return start; } }
Iterator^ Container first() beyond() next() …
operator * operator ++ …
template< class CT, class ET > template< class CT, class ET >
Iterator< CT, ET >::Iterator( Iterator< CT, ET >::Iterator( Iterator< CT, ET >& it ) { Iterator< CT, ET >& it ) { container = it.container; container = it.container; index = it.index; index = it.index;
} }
template< class CT, class ET > template< class CT, class ET >
ET& Iterator< CT, ET >::operator *() ET& Iterator< CT, ET >::operator *()
{ {
return *index; return *index;
} }