| qlinkedlist.cpp |
! \class QLinkedList
\inmodule QtCore5Compat
\brief The QLinkedList class is a template class that provides linked lists.
\ingroup tools
\ingroup shared
\reentrant
QLinkedList\<T\> is one of Qt's generic \l{container classes}, where
\a T is the type of elements stored in the list. It stores a list of
values and provides iterator-based access as well as \l{constant time}
insertions and removals.
QList\<T\> and QLinkedList\<T\> provide similar functionality.
Here's an overview:
\list
\li For most purposes, QList is the right class to use. Its
index-based API is more convenient than QLinkedList's
iterator-based API. Its items occupy adjacent memory positions.
It also expands to less code in your executable.
\li If you need a real linked list, with guarantees of \l{constant
time} insertions in the middle of the list and iterators to
items rather than indexes, use QLinkedList.
\endlist
Here's an example of a QLinkedList that stores integers and a
QLinkedList that stores QTime values:
\snippet code/src_corelib_tools_qlinkedlist.cpp 0
QLinkedList stores a list of items. The default constructor
creates an empty list. To insert items into the list, you can use
operator<<():
\snippet code/src_corelib_tools_qlinkedlist.cpp 1
If you want to get the first or last item in a linked list, use
first() or last(). If you want to remove an item from either end
of the list, use removeFirst() or removeLast(). If you want to
remove all occurrences of a given value in the list, use
removeAll().
A common requirement is to remove the first or last item in the
list and do something with it. For this, QLinkedList provides
takeFirst() and takeLast(). Here's a loop that removes the items
from a list one at a time and calls \c delete on them:
\snippet code/src_corelib_tools_qlinkedlist.cpp 2
QLinkedList's value type must be an \l {assignable data type}. This
covers most data types that are commonly used, but the compiler
won't let you, for example, store a QWidget as a value; instead,
store a QWidget *. A few functions have additional requirements;
for example, contains() and removeAll() expect the value type to
support \c operator==(). These requirements are documented on a
per-function basis.
If you want to insert, modify, or remove items in the middle of
the list, you must use an iterator. QLinkedList provides both
\l{Java-style iterators} (QLinkedListIterator and
QMutableLinkedListIterator) and \l{STL-style iterators}
(QLinkedList::const_iterator and QLinkedList::iterator). See the
documentation for these classes for details.
\sa QLinkedListIterator, QMutableLinkedListIterator, QList
|
33641 |
- |