QLinkedList Class

Detailed Description

QLinkedList<T> is one of Qt's generic container classes. It stores a list of values and provides iterator-based access as well as constant time insertions and removals.

QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:

  • For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory (see Algorithmic Complexity for details). It also expands to less code in your executable.

  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.

  • If you want the items to occupy adjacent memory positions, use QVector.

Here's an example of a QLinkedList that stores integers and a QLinkedList that stores QTime values:

 
Sélectionnez
QLinkedList&lt;int&gt; integerList;
QLinkedList&lt;QTime&gt; timeList;

QLinkedList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():

 
Sélectionnez
QLinkedList&lt;QString&gt; list;
list &lt;&lt; "one" &lt;&lt; "two" &lt;&lt; "three";
// list: ["one", "two", "three"]

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 delete on them:

 
Sélectionnez
QLinkedList&lt;QWidget *&gt; list;
...
while (!list.isEmpty())
    delete list.takeFirst();

QLinkedList's value type must be an 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 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 Java-style iterators (QLinkedListIterator and QMutableLinkedListIterator) and STL-style iterators (QLinkedList::const_iterator and QLinkedList::iterator). See the documentation for these classes for details.

See Also

See also QLinkedListIterator, QMutableLinkedListIterator, QList, QVector

Member Type Documentation

 

QLinkedList::ConstIterator

Qt-style synonym for QLinkedList::const_iterator.

QLinkedList::Iterator

Qt-style synonym for QLinkedList::iterator.

QLinkedList::const_pointer

Typedef for const T *. Provided for STL compatibility.

QLinkedList::const_reference

Typedef for const T &. Provided for STL compatibility.

[since 5.6] QLinkedList::const_reverse_iterator

The QLinkedList::const_reverse_iterator typedef provides an STL-style const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::const_iterator>.

Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See Also

QLinkedList::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

QLinkedList::pointer

Typedef for T *. Provided for STL compatibility.

QLinkedList::reference

Typedef for T &. Provided for STL compatibility.

[since 5.6] QLinkedList::reverse_iterator

The QLinkedList::reverse_iterator typedef provides an STL-style non-const reverse iterator for QLinkedList.

It is simply a typedef for std::reverse_iterator<QLinkedList::iterator>.

Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read Implicit sharing iterator problem.

This typedef was introduced in Qt 5.6.

See Also

QLinkedList::size_type

Typedef for int. Provided for STL compatibility.

QLinkedList::value_type

Typedef for T. Provided for STL compatibility.

Member Function Documentation

 

QLinkedList::QLinkedList()

Constructs an empty list.

QLinkedList::QLinkedList(const QLinkedList<T> &other)

Constructs a copy of other.

This operation occurs in constant time, because QLinkedList is implicitly shared. This makes returning a QLinkedList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes linear time.

See Also

See also operator=()

[since 5.2] QLinkedList::QLinkedList(std::initializer_list<T> list)

Constructs a list from the std::initializer_list specified by list.

This constructor is only enabled if the compiler supports C++11 initializer lists.

This function was introduced in Qt 5.2.

[since 5.14] QLinkedList::QLinkedList(InputIterator first, InputIterator last)

Constructs a list with the contents in the iterator range [first, last).

The value type of InputIterator must be convertible to T.

This function was introduced in Qt 5.14.

[since 5.2] QLinkedList::QLinkedList(QLinkedList<T> &&other)

Move-constructs a QLinkedList instance, making it point at the same object that other was pointing to.

This function was introduced in Qt 5.2.

QLinkedList::~QLinkedList()

Destroys the list. References to the values in the list, and all iterators over this list, become invalid.

void QLinkedList::append(const T &value)

Inserts value at the end of the list.

Example:

 
Sélectionnez
QLinkedList&lt;QString&gt; list;
list.append("one");
list.append("two");
list.append("three");
// list: ["one", "two", "three"]

This is the same as list.insert(end(), value).

See Also

See also operator<<(), prepend(), insert()

T &QLinkedList::back()