![]() |
| ||
Classes - Annotated - Tree - Functions - Home - Structure |
The QPtrListIterator class provides an iterator for QPtrList collections. More...
#include <qptrlist.h>
Inherited by QStrListIterator.
Define a template instance QPtrListIterator<X> to create a list iterator that operates on QPtrList<X> (list of X*).
Example:
#include <qptrlist.h> #include <qstring.h> #include <stdio.h> class Employee { public: Employee( const char *name, int salary ) { n=name; s=salary; } const char *name() const { return n; } int salary() const { return s; } private: QString n; int s; }; int main() { QPtrList<Employee> list; // list of pointers to Employee list.setAutoDelete( TRUE ); // delete items when they are removed list.append( new Employee("Bill", 50000) ); list.append( new Employee("Steve",80000) ); list.append( new Employee("Ron", 60000) ); QPtrListIterator<Employee> it(list); // iterator for employee list for ( ; it.current(); ++it ) { Employee *emp = it.current(); printf( "%s earns %d\n", emp->name().latin1(), emp->salary() ); } return 0; }
Program output:
Bill earns 50000 Steve earns 80000 Ron earns 60000
Although QPtrList has member functions to traverse the doubly-linked list structure, using a list iterator is a much more robust way of traversing the list because multiple list iterators can operate on the same list, independent of each other and independent of the QPtrList's current item. An iterator has its own current list item and can get the next and previous list items. It can only traverse the list, never modify it.
A QPtrList knows about all list iterators that are operating on the list. When an item is removed from the list, the list updates all iterators pointing the removed item to point to the new current list item.
Example:
#include <qptrlist.h> #include <qstring.h> #include <stdio.h> class Employee { ... // same as above }; int main() { QPtrList<Employee> list; // list of pointers to Employee list.setAutoDelete( TRUE ); // delete items when they are removed list.append( new Employee("Bill", 50000) ); list.append( new Employee("Steve",80000) ); list.append( new Employee("Ron", 60000) ); QPtrListIterator<Employee> it(list); list.at( 1 ); // current list item: "Steve" it.toLast(); // it: "Ron" --it; // it: "Steve" // Now, both the list and the iterator are referring the same item list.remove(); printf( "%s\n", it.current()->name().latin1() ); return 0; }
Program output:
Ron
See also QPtrList and collection classes.
See also toFirst() and atLast().
See also toLast() and atFirst().
See also isEmpty().
Example: customlayout/card.cpp.
Examples: customlayout/card.cpp and customlayout/flow.cpp.
See also count().
If the current iterator item was the last item in the list or if it was null, null is returned.
If the current iterator item was the last item in the list or if it was null, null is returned.
If that item is beyond the last item or if the dictionary is empty, it sets the current item to null and returns null
If the current iterator item was the first item in the list or if it was null, null is returned.
See also toLast() and atFirst().
See also toFirst() and atLast().
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.
Copyright © 2001 Trolltech | Trademarks | Qt version 3.0.0-beta1-beta1
|