3.2. Vector<T>::iterator

This class provides an STL style random access iterator for use with generic algorithms. Since neither the vector nor the matrix classes are, strictly speaking, container classes, the iterator class is really an iterator of the data object that is viewed by a vector or matrix. Note that if any operation occurs that would cause the memory this iterator references to be deallocated, the iterator becomes invalidated. No bounds-checking is done for iterators, nor is any checking done to make sure that the arguments for relational operators refer to the same object.

3.2.1. Constructors.

explicit Vector<T>::iterator(T* p = 0, ptrdiff_t s = 1)

This creates an iterator that points to the value pointed to by p, with stride s.

Vector<T>::iterator(const Vector<T>::iterator& rhs)

The copy constructor creates a new iterator, and sets it equal to rhs.

Vector<T>::~iterator()

This destroys the iterator.

3.2.2. Overloaded operators.

Vector<T>::iterator& operator++()

The prefix increment operator moves the iterator to the next element of the object it is referencing, according to the stride of the object from which it was created.

Vector<T>::iterator& operator--()

The prefix decrement operator. This functions just like the increment operator, except that it steps through the data backwards.

Vector<T>::iterator operator++(int)

The postfix increment operator.

Vector<T>::iterator operator--(int)

The postfix decrement operator.

Vector<T>::iterator& operator+=(ptrdiff_t n)

This increments the iterator by n elements. The result is equivalent to using the increment operator n times, but much more efficient.

Vector<T>::iterator& operator-=(ptrdiff_t n)

This decrements the iterator by n elements.

Vector<T>::iterator& operator=(const Vector<T>::iterator& rhs)

The assignment operator sets the iterator to reference the same data as rhs, and points the iterator to the same element as rhs.

ptrdiff_t operator-(const Vector<T>::iterator& rhs) const

This returns the difference between two iterators. This operation is only well defined if the two iterators reference the same data in the same way.

Vector<T>::iterator operator-(ptrdiff_t n) const

This returns an iterator pointing to n elements before the iterator it is called for.

Vector<T>::iterator operator+(ptrdiff_t n) const

This returns an iterator pointing to n elements after the iterator it is called for.

T& operator*() const

The dereferencing operator returns a reference to the element at which the iterator is pointing.

T* operator->() const

The member selection operator returns a pointer to the element at which the iterator is pointing.

T& operator[](size_t n) const

The indexing operator returns a reference to the element n elements after the element pointed to by the iterator.

bool operator==(const Vector<T>::iterator& rhs) const

This returns true if the iterator it is called for points to the same element as rhs, and false if it does not. It is undefined if the two iterators reference the data in different ways, or if they reference different data. This is also the case for the other relation operators that follow.

bool operator!=(const Vector<T>::iterator& rhs) const

bool operator<(const Vector<T>::iterator& rhs) const

bool operator>(const Vector<T>::iterator& rhs) const

bool operator<=(const Vector<T>::iterator& rhs) const

bool operator>=(const Vector<T>::iterator& rhs) const

These relation operators all work as would be expected based on the behavior of operator==().

3.3. Vector<T>::const_iterator

This class functions almost identically to the iterator class. The main difference is that it cannot be dereferenced or indexed in a way that would alter the data it references. A constant iterator can be made from a constant or non-constant vector or matrix, but the ordinary iterators can only be made from non-constant vectors and matrices.

3.3.1. Constructors.

explicit Vector<T>::const_iterator(T* p = 0, ptrdiff_t s = 1)

This creates a constant iterator, which points to the value pointed to by p, with stride s.

Vector<T>::const_iterator(const Vector<T>::const_iterator& rhs)

The copy constructor creates a new constant iterator, and sets it equal to rhs.

Vector<T>::const_iterator(const Vector<T>::iterator& rhs)

This constructor creates a new constant iterator from an ordinary iterator.

Vector<T>::~const_iterator()

This destroys the iterator.

3.3.2. Overloaded operators.

Vector<T>::const_iterator& operator++()

The prefix increment operator moves the iterator to the next element of the object it is referencing, according to the stride of the object from which it was created.

Vector<T>::const_iterator& operator--()

The prefix decrement operator. This functions just like the increment operator, except that it steps through the data backwards.

Vector<T>::const_iterator operator++(int)

The postfix increment operator.

Vector<T>::const_iterator operator--(int)

The postfix decrement operator.

Vector<T>::const_iterator& operator+=(ptrdiff_t n)

This increments the iterator by n elements. The result is equivalent to using the increment operator n times, but much more efficient.

Vector<T>::const_iterator& operator-=(ptrdiff_t n)

This decrements the iterator by n elements.

Vector<T>::const_iterator& operator=(const Vector<T>::const_iterator& rhs)

The assignment operator sets the iterator to reference the same data as rhs, and points the iterator to the same element as rhs.

Vector<T>::const_iterator& operator=(const Vector<T>::iterator& rhs)

This assignment operator sets the constant iterator to reference the same data as the ordinary iterator rhs, and points the iterator to the same element as rhs.

ptrdiff_t operator-(const Vector<T>::const_iterator& rhs) const

This returns the difference between two iterators. This operation is only well defined if the two iterators reference the same data in the same way.

Vector<T>::const_iterator operator-(ptrdiff_t n) const

This returns an iterator pointing to n elements before the iterator it is called for.

Vector<T>::const_iterator operator+(ptrdiff_t n) const

This returns an iterator pointing to n elements after the iterator it is called for.

const T& operator*() const

The dereferencing operator returns a constant reference to the element at which the iterator is pointing. This returned reference cannot be modified.

const T* operator->() const

The member selection operator returns a constant pointer to the element at which the iterator is pointing.

const T& operator[](size_t n) const

The indexing operator returns a constant reference to the element n elements after the element pointed to by the iterator. This returned reference cannot be modified.

bool operator==(const Vector<T>::const_iterator& rhs) const

This returns true if the iterator it is called for points to the same element as rhs, and false if it does not. It is undefined if the two iterators reference the data in different ways, or if they reference different data. This is also the case for the other relation operators that follow.

bool operator!=(const Vector<T>::const_iterator& rhs) const

bool operator<(const Vector<T>::const_iterator& rhs) const

bool operator>(const Vector<T>::const_iterator& rhs) const

bool operator<=(const Vector<T>::const_iterator& rhs) const

bool operator>=(const Vector<T>::const_iterator& rhs) const

These relation operators all work as would be expected based on the behavior of operator==().

Next Section

Back to Classes