set05 - private: Vector *v; int index; public:...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Jan. 2000 - ENEL 409 Dr. M. Moussavi 1 C++ Iterators C++ Iterators
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Jan. 2000 - ENEL 409 Dr. M. Moussavi 2 Iterators Iterators In general an iterator provides a general method of successively accessing each element within any of the sequential or associative types.
Background image of page 2
Jan. 2000 - ENEL 409 Dr. M. Moussavi 3 Example Example class Vector { friend class VectIter; private: int *array; int size; public: Vector(int sz); int& operator[](int i){return array[i];} };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Jan. 2000 - ENEL 409 Dr. M. Moussavi 4 Example continued Example continued Vector::Vector(int sz) { size=sz; array=new int[sz]; assert (array != NULL); cout <<endl << "Building a vector with "<< sz << "elements is in progress:\n"; for (int i=0;i<size;i++) { cout << "\nEnter an integer value for Vector["<<i <<"]: "; cin >> array[i]; } }
Background image of page 4
Jan. 2000 - ENEL 409 Dr. M. Moussavi 5 Example continued Example continued class VectIter{
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: private: Vector *v; int index; public: VectIter(Vector&amp; x){ v = &amp;x ; index=0; } int operator()(int dir); int operator *(); int operator++(); int operator++(int); int operator--(); int operator--(int); }; Jan. 2000 - ENEL 409 Dr. M. Moussavi 6 Iterator Operators Iterator Operators int VectIter::operator *() { return v-&gt;array[index]; } int VectIter::operator ++() { index++; return v-&gt;array[index]; } Jan. 2000 - ENEL 409 Dr. M. Moussavi 7 Iterator Operators Iterator Operators int VectIter::operator ++(int) { int value = v-&gt;array[index]; index++; return value; } Jan. 2000 - ENEL 409 Dr. M. Moussavi 8 Using Iterators Using Iterators main() { Vector x(10); VectIter iter(x); cout &lt;&lt; endl &lt;&lt; *iter; cout &lt;&lt; endl &lt;&lt; ++iter; cout &lt;&lt; endl &lt;&lt; iter ++; cin.get(); cin.get(); return 0; }...
View Full Document

Page1 / 8

set05 - private: Vector *v; int index; public:...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online