QList< T >::iterator Class Reference

#include <qlist.h>

Collaboration diagram for QList< T >::iterator:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<typename T>
class QList< T >::iterator

The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.

QList features both {STL-style iterators} and {Java-style iterators}. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.

QList<T>iterator allows you to iterate over a QList<T> (or QQueue<T>) and to modify the list item associated with the iterator. If you want to iterate over a const QList, use QList::const_iterator instead. It is generally good practice to use QList::const_iterator on a non-const QList as well, unless you need to change the QList through the iterator. Const iterators are slightly faster, and can improve code readability.

The default QList::iterator constructor creates an uninitialized iterator. You must initialize it using a QList function like QList::begin(), QList::end(), or QList::insert() before you can start iterating. Here's a typical loop that prints all the items stored in a list:

        QList<QString> list;
        list.append("January");
        list.append("February");
        ...
        list.append("December");

        QList<QString>::iterator i;
        for (i = list.begin(); i != list.end(); ++i)
            cout << *i << endl;

Let's see a few examples of things we can do with a QList::iterator that we cannot do with a QList::const_iterator. Here's an example that increments every value stored in a QList<int> by 2:

        QList<int>::iterator i;
        for (i = list.begin(); i != list.end(); ++i)
            *i += 2;

Most QList functions accept an integer index rather than an iterator. For that reason, iterators are rarely useful in connection with QList. One place where STL-style iterators do make sense is as arguments to {generic algorithms}.

For example, here's how to delete all the widgets stored in a QList<QWidget *>:

        QList<QWidget *> list;
        ...
        qDeleteAll(list.begin(), list.end());

Multiple iterators can be used on the same list. However, be aware that any non-const function call performed on the QList will render all existing iterators undefined. If you need to keep iterators over a long period of time, we recommend that you use QLinkedList rather than QList.

See also:
QList::const_iterator, QMutableListIterator

Definition at line 128 of file qlist.h.

Public Types

typedef std::random_access_iterator_tag iterator_category
typedef ptrdiff_t difference_type
typedef T value_type
typedef T * pointer
typedef T & reference

Public Member Functions

 iterator ()
 iterator (Node *n)
 iterator (const iterator &o)
T & operator * () const
T * operator-> () const
T & operator[] (int j) const
bool operator== (const iterator &o) const
bool operator!= (const iterator &o) const
bool operator< (const iterator &other) const
bool operator<= (const iterator &other) const
bool operator> (const iterator &other) const
bool operator>= (const iterator &other) const
bool operator== (const const_iterator &o) const
bool operator!= (const const_iterator &o) const
bool operator< (const const_iterator &other) const
bool operator<= (const const_iterator &other) const
bool operator> (const const_iterator &other) const
bool operator>= (const const_iterator &other) const
iteratoroperator++ ()
iterator operator++ (int)
iteratoroperator-- ()
iterator operator-- (int)
iteratoroperator+= (int j)
iteratoroperator-= (int j)
iterator operator+ (int j) const
iterator operator- (int j) const
int operator- (iterator j) const

Public Attributes

Nodei


Member Typedef Documentation

template<typename T>
QList< T >::iterator::iterator_category

Definition at line 131 of file qlist.h.

template<typename T>
QList< T >::iterator::difference_type

Definition at line 132 of file qlist.h.

template<typename T>
QList< T >::iterator::value_type

Definition at line 133 of file qlist.h.

template<typename T>
QList< T >::iterator::pointer

Definition at line 134 of file qlist.h.

template<typename T>
QList< T >::iterator::reference

Definition at line 135 of file qlist.h.


Constructor & Destructor Documentation

template<typename T>
QList< T >::iterator::iterator (  )  [inline]

Constructs an uninitialized iterator.

Functions like operator*() and operator++() should not be called on an uninitialized iterartor. Use operator=() to assign a value to it before using it.

See also:
QList::begin() QList::end()

Definition at line 137 of file qlist.h.

Referenced by QList< T >::iterator::operator+(), and QList< T >::iterator::operator-().

00137 : i(0) {}

template<typename T>
QList< T >::iterator::iterator ( Node n  )  [inline]

Definition at line 138 of file qlist.h.

00138 : i(n) {}

template<typename T>
QList< T >::iterator::iterator ( const iterator other  )  [inline]

Constructs a copy of other.

Definition at line 139 of file qlist.h.

00139 : i(o.i){}


Member Function Documentation

template<typename T>
T & QList< T >::iterator::operator * (  )  const [inline]

Returns a modifiable reference to the current item.

You can change the value of an item by using operator*() on the left side of an assignment, for example:

        if (*it == "Hello")
            *it = "Bonjour";

See also:
operator->()

Definition at line 140 of file qlist.h.

References QList< T >::iterator::i, and QList< T >::Node::t().

00140 { return i->t(); }

Here is the call graph for this function:

template<typename T>
T * QList< T >::iterator::operator-> (  )  const [inline]

Returns a pointer to the current item.

See also:
operator*()

Definition at line 141 of file qlist.h.

References QList< T >::iterator::i, and QList< T >::Node::t().

00141 { return &i->t(); }

Here is the call graph for this function:

template<typename T>
T & QList< T >::iterator::operator[] ( int  j  )  const [inline]

Returns a modifiable reference to the item at position *this + {j}.

This function is provided to make QList iterators behave like C++ pointers.

See also:
operator+()

Definition at line 142 of file qlist.h.

References QList< T >::iterator::i, and QList< T >::Node::t().

00142 { return i[j].t(); }

Here is the call graph for this function:

template<typename T>
bool QList< T >::iterator::operator== ( const iterator o  )  const [inline]

Definition at line 143 of file qlist.h.

References QList< T >::iterator::i, and o.

00143 { return i == o.i; }

template<typename T>
bool QList< T >::iterator::operator!= ( const iterator o  )  const [inline]

Definition at line 144 of file qlist.h.

References QList< T >::iterator::i, and o.

00144 { return i != o.i; }

template<typename T>
bool QList< T >::iterator::operator< ( const iterator other  )  const [inline]

Definition at line 145 of file qlist.h.

References QList< T >::iterator::i.

00145 { return i < other.i; }

template<typename T>
bool QList< T >::iterator::operator<= ( const iterator other  )  const [inline]

Definition at line 146 of file qlist.h.

References QList< T >::iterator::i.

00146 { return i <= other.i; }

template<typename T>
bool QList< T >::iterator::operator> ( const iterator other  )  const [inline]

Definition at line 147 of file qlist.h.

References QList< T >::iterator::i.

00147 { return i > other.i; }

template<typename T>
bool QList< T >::iterator::operator>= ( const iterator other  )  const [inline]

Definition at line 148 of file qlist.h.

References QList< T >::iterator::i.

00148 { return i >= other.i; }

template<typename T>
bool QList< T >::iterator::operator== ( const const_iterator other  )  const [inline]

Returns true if other points to the same item as this iterator; otherwise returns false.

See also:
operator!=()

Definition at line 150 of file qlist.h.

References QList< T >::iterator::i, and o.

00151             { return i == reinterpret_cast<const iterator &>(o).i; }

template<typename T>
bool QList< T >::iterator::operator!= ( const const_iterator other  )  const [inline]

Returns true if other points to a different item than this iterator; otherwise returns false.

See also:
operator==()

Definition at line 152 of file qlist.h.

References QList< T >::iterator::i, and o.

00153             { return i != reinterpret_cast<const iterator &>(o).i; }

template<typename T>
bool QList< T >::iterator::operator< ( const const_iterator other  )  const [inline]

Returns true if the item pointed to by this iterator is less than the item pointed to by the other iterator.

Definition at line 154 of file qlist.h.

References QList< T >::iterator::i.

00155             { return i < reinterpret_cast<const iterator &>(other).i; }

template<typename T>
bool QList< T >::iterator::operator<= ( const const_iterator other  )  const [inline]

Returns true if the item pointed to by this iterator is less than or equal to the item pointed to by the other iterator.

Definition at line 156 of file qlist.h.

References QList< T >::iterator::i.

00157             { return i <= reinterpret_cast<const iterator &>(other).i; }

template<typename T>
bool QList< T >::iterator::operator> ( const const_iterator other  )  const [inline]

Returns true if the item pointed to by this iterator is greater than the item pointed to by the other iterator.

Definition at line 158 of file qlist.h.

References QList< T >::iterator::i.

00159             { return i > reinterpret_cast<const iterator &>(other).i; }

template<typename T>
bool QList< T >::iterator::operator>= ( const const_iterator other  )  const [inline]

Returns true if the item pointed to by this iterator is greater than or equal to the item pointed to by the other iterator.

Definition at line 160 of file qlist.h.

References QList< T >::iterator::i.

00161             { return i >= reinterpret_cast<const iterator &>(other).i; }

template<typename T>
QList::iterator & QList< T >::iterator::operator++ (  )  [inline]

The prefix ++ operator ({++it}) advances the iterator to the next item in the list and returns an iterator to the new current item.

Calling this function on QList::end() leads to undefined results.

See also:
operator--()

Definition at line 163 of file qlist.h.

References QList< T >::iterator::i.

00163 { ++i; return *this; }

template<typename T>
QList::iterator QList< T >::iterator::operator++ ( int   )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. The postfix ++ operator ({it++}) advances the iterator to the next item in the list and returns an iterator to the previously current item.

Definition at line 164 of file qlist.h.

References QList< T >::iterator::i, and n.

00164 { Node *n = i; ++i; return n; }

template<typename T>
QList::iterator & QList< T >::iterator::operator-- (  )  [inline]

The prefix -- operator ({--it}) makes the preceding item current and returns an iterator to the new current item.

Calling this function on QList::begin() leads to undefined results.

See also:
operator++()

Definition at line 165 of file qlist.h.

References QList< T >::iterator::i.

00165 { i--; return *this; }

template<typename T>
QList::iterator QList< T >::iterator::operator-- ( int   )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. The postfix -- operator ({it--}) makes the preceding item current and returns an iterator to the previously current item.

Definition at line 166 of file qlist.h.

References QList< T >::iterator::i, and n.

00166 { Node *n = i; i--; return n; }

template<typename T>
QList::iterator & QList< T >::iterator::operator+= ( int  j  )  [inline]

Advances the iterator by j items. (If j is negative, the iterator goes backward.)

See also:
operator-=(), operator+()

Definition at line 167 of file qlist.h.

References QList< T >::iterator::i.

00167 { i+=j; return *this; }

template<typename T>
QList::iterator & QList< T >::iterator::operator-= ( int  j  )  [inline]

Makes the iterator go back by j items. (If j is negative, the iterator goes forward.)

See also:
operator+=(), operator-()

Definition at line 168 of file qlist.h.

References QList< T >::iterator::i.

00168 { i-=j; return *this; }

template<typename T>
QList::iterator QList< T >::iterator::operator+ ( int  j  )  const [inline]

Returns an iterator to the item at j positions forward from this iterator. (If j is negative, the iterator goes backward.)

See also:
operator-(), operator+=()

Definition at line 169 of file qlist.h.

References QList< T >::iterator::i, and QList< T >::iterator::iterator().

00169 { return iterator(i+j); }

Here is the call graph for this function:

template<typename T>
QList::iterator QList< T >::iterator::operator- ( int  j  )  const [inline]

Returns an iterator to the item at j positions backward from this iterator. (If j is negative, the iterator goes forward.)

See also:
operator+(), operator-=()

Definition at line 170 of file qlist.h.

References QList< T >::iterator::i, and QList< T >::iterator::iterator().

00170 { return iterator(i-j); }

Here is the call graph for this function:

template<typename T>
int QList< T >::iterator::operator- ( iterator  other  )  const [inline]

Returns the number of items between the item pointed to by other and the item pointed to by this iterator.

Definition at line 171 of file qlist.h.

References QList< T >::iterator::i, and j.

00171 { return i - j.i; }


Member Data Documentation

template<typename T>
Node* QList< T >::iterator::i

Definition at line 130 of file qlist.h.

Referenced by QList< T >::erase(), QList< T >::insert(), QList< T >::iterator::operator *(), QList< T >::iterator::operator!=(), QList< T >::iterator::operator+(), QList< T >::iterator::operator++(), QList< T >::iterator::operator+=(), QList< T >::iterator::operator-(), QList< T >::iterator::operator--(), QList< T >::iterator::operator-=(), QList< T >::iterator::operator->(), QList< T >::iterator::operator<(), QList< T >::iterator::operator<=(), QList< T >::iterator::operator==(), QList< T >::iterator::operator>(), QList< T >::iterator::operator>=(), and QList< T >::iterator::operator[]().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:16:53 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1