The QLinkedList class is a template class that provides linked lists. More...
Header: | #include <QLinkedList> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core5Compat) target_link_libraries(mytarget PRIVATE Qt6::Core5Compat) |
qmake: | QT += core5compat |
Note: All functions in this class are reentrant.
class | const_iterator |
class | iterator |
ConstIterator | |
Iterator | |
const_pointer | |
const_reference | |
const_reverse_iterator | |
difference_type | |
pointer | |
reference | |
reverse_iterator | |
size_type | |
value_type |
QLinkedList(QLinkedList<T> &&other) | |
QLinkedList(InputIterator first, InputIterator last) | |
QLinkedList(std::initializer_list<T> list) | |
QLinkedList(const QLinkedList<T> &other) | |
QLinkedList() | |
QLinkedList<T> & | operator=(const QLinkedList<T> &other) |
~QLinkedList() | |
void | append(const T &value) |
T & | back() |
const T & | back() const |
QLinkedList::iterator | begin() |
QLinkedList::const_iterator | begin() const |
QLinkedList::const_iterator | cbegin() const |
QLinkedList::const_iterator | cend() const |
void | clear() |
QLinkedList::const_iterator | constBegin() const |
QLinkedList::const_iterator | constEnd() const |
bool | contains(const T &value) const |
int | count(const T &value) const |
int | count() const |
QLinkedList::const_reverse_iterator | crbegin() const |
QLinkedList::const_reverse_iterator | crend() const |
bool | empty() const |
QLinkedList::iterator | end() |
QLinkedList::const_iterator | end() const |
bool | endsWith(const T &value) const |
QLinkedList::iterator | erase(QLinkedList::iterator pos) |
QLinkedList::iterator | erase(QLinkedList::iterator begin, QLinkedList::iterator end) |
T & | first() |
const T & | first() const |
T & | front() |
const T & | front() const |
QLinkedList::iterator | insert(QLinkedList::iterator before, const T &value) |
bool | isEmpty() const |
T & | last() |
const T & | last() const |
void | pop_back() |
void | pop_front() |
void | prepend(const T &value) |
void | push_back(const T &value) |
void | push_front(const T &value) |
QLinkedList::reverse_iterator | rbegin() |
QLinkedList::const_reverse_iterator | rbegin() const |
int | removeAll(const T &value) |
void | removeFirst() |
void | removeLast() |
bool | removeOne(const T &value) |
QLinkedList::reverse_iterator | rend() |
QLinkedList::const_reverse_iterator | rend() const |
int | size() const |
bool | startsWith(const T &value) const |
void | swap(QLinkedList<T> &other) |
T | takeFirst() |
T | takeLast() |
std::list<T> | toStdList() const |
bool | operator!=(const QLinkedList<T> &other) const |
QLinkedList<T> | operator+(const QLinkedList<T> &other) const |
QLinkedList<T> & | operator+=(const QLinkedList<T> &other) |
QLinkedList<T> & | operator+=(const T &value) |
QLinkedList<T> & | operator<<(const QLinkedList<T> &other) |
QLinkedList<T> & | operator<<(const T &value) |
bool | operator==(const QLinkedList<T> &other) const |
QLinkedList<T> | fromStdList(const std::list<T> &list) |
QDataStreamIfHasOStreamOperatorsContainer<QLinkedList<T>, T> | operator<<(QDataStream &out, const QLinkedList<T> &list) |
QDataStreamIfHasIStreamOperatorsContainer<QLinkedList<T>, T> | operator>>(QDataStream &in, QLinkedList<T> &list) |
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> and QLinkedList<T> provide similar functionality. Here's an overview:
Here's an example of a QLinkedList that stores integers and a QLinkedList that stores QTime values:
QLinkedList<int> integerList; QLinkedList<QTime> timeList;
QLinkedList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():
QLinkedList<QString> list; list << "one" << "two" << "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:
QLinkedList<QWidget *> 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 QLinkedListIterator, QMutableLinkedListIterator, and QList.
Qt-style synonym for QLinkedList::const_iterator.
Qt-style synonym for QLinkedList::iterator.
Typedef for const T *. Provided for STL compatibility.
Typedef for const T &. Provided for STL compatibility.
[since 5.6]
QLinkedList::const_reverse_iteratorThe 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>
.
Warning: 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::rbegin(), QLinkedList::rend(), QLinkedList::reverse_iterator, and QLinkedList::const_iterator.
Typedef for ptrdiff_t. Provided for STL compatibility.
Typedef for T *. Provided for STL compatibility.
Typedef for T &. Provided for STL compatibility.
[since 5.6]
QLinkedList::reverse_iteratorThe 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>
.
Warning: 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::rbegin(), QLinkedList::rend(), QLinkedList::const_reverse_iterator, and QLinkedList::iterator.
Typedef for int. Provided for STL compatibility.
Typedef for T. Provided for STL compatibility.
[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.
[since 5.14]
template <typename InputIterator> 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(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.
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 operator=().
Constructs an empty list.
Assigns other to this list and returns a reference to this list.
Destroys the list. References to the values in the list, and all iterators over this list, become invalid.
Inserts value at the end of the list.
Example:
QLinkedList<QString> 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 operator<<(), prepend(), and insert().
This function is provided for STL compatibility. It is equivalent to last().
This is an overloaded function.
Returns an STL-style iterator pointing to the first item in the list.
See also constBegin() and end().
This is an overloaded function.
[since 5.0]
QLinkedList::const_iterator QLinkedList::cbegin() constReturns a const STL-style iterator pointing to the first item in the list.
This function was introduced in Qt 5.0.
[since 5.0]
QLinkedList::const_iterator QLinkedList::cend() constReturns a const STL-style iterator pointing to the imaginary item after the last item in the list.
This function was introduced in Qt 5.0.
Removes all the items in the list.
See also removeAll().
Returns a const STL-style iterator pointing to the first item in the list.
See also begin() and constEnd().
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
See also constBegin() and end().
Returns true
if the list contains an occurrence of value; otherwise returns false
.
This function requires the value type to have an implementation of operator==()
.
See also QLinkedListIterator::findNext() and QLinkedListIterator::findPrevious().
Returns the number of occurrences of value in the list.
This function requires the value type to have an implementation of operator==()
.
See also contains().
Same as size().
[since 5.6]
QLinkedList::const_reverse_iterator QLinkedList::crbegin() constReturns a const STL-style reverse iterator pointing to the first item in the list, in reverse order.
This function was introduced in Qt 5.6.
See also begin(), rbegin(), and rend().
[since 5.6]
QLinkedList::const_reverse_iterator QLinkedList::crend()
constReturns a const STL-style reverse iterator pointing to one past the last item in the list, in reverse order.
This function was introduced in Qt 5.6.
See also end(), rend(), and rbegin().
This function is provided for STL compatibility. It is equivalent to isEmpty() and returns true
if the list is empty.
Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
See also begin() and constEnd().
This is an overloaded function.
Returns true
if the list is not empty and its last item is equal to value; otherwise returns false
.
See also isEmpty() and last().
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).
See also insert().
This is an overloaded function.
Removes all the items from begin up to (but not including) end.
Returns a reference to the first item in the list. This function assumes that the list isn't empty.
See also last() and isEmpty().
This is an overloaded function.
[static]
QLinkedList<T> QLinkedList::fromStdList(const std::list<T> &list)Returns a QLinkedList object with the data contained in list. The order of the elements in the QLinkedList is the same as in list.
Example:
std::list<double> stdlist; list.push_back(1.2); list.push_back(0.5); list.push_back(3.14); QLinkedList<double> list = QLinkedList<double>::fromStdList(stdlist);
See also toStdList().
This function is provided for STL compatibility. It is equivalent to first().
This is an overloaded function.
Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.
See also erase().
Returns true
if the list contains no items; otherwise returns false.
See also size().
Returns a reference to the last item in the list. This function assumes that the list isn't empty.
See also first() and isEmpty().
This is an overloaded function.
This function is provided for STL compatibility. It is equivalent to removeLast().
This function is provided for STL compatibility. It is equivalent to removeFirst().
Inserts value at the beginning of the list.
Example:
QLinkedList<QString> list; list.prepend("one"); list.prepend("two"); list.prepend("three"); // list: ["three", "two", "one"]
This is the same as list.insert(begin(), value).
See also append() and insert().
This function is provided for STL compatibility. It is equivalent to append(value).
This function is provided for STL compatibility. It is equivalent to prepend(value).
[since 5.6]
QLinkedList::reverse_iterator QLinkedList::rbegin()Returns a STL-style reverse iterator pointing to the first item in the list, in reverse order.
This function was introduced in Qt 5.6.
See also begin(), crbegin(), and rend().
[since 5.6]
QLinkedList::const_reverse_iterator QLinkedList::rbegin() constThis is an overloaded function.
This function was introduced in Qt 5.6.
Removes all occurrences of value in the list.
Example:
QList<QString> list; list << "sun" << "cloud" << "sun" << "rain"; list.removeAll("sun"); // list: ["cloud", "rain"]
This function requires the value type to have an implementation of operator==()
.
See also insert().
Removes the first item in the list.
This is the same as erase(begin()).
See also removeLast() and erase().
Removes the last item in the list.
See also removeFirst() and erase().
Removes the first occurrences of value in the list. Returns true
on success; otherwise returns false
.
Example:
QList<QString> list; list << "sun" << "cloud" << "sun" << "rain"; list.removeOne("sun"); // list: ["cloud", "sun", "rain"]
This function requires the value type to have an implementation of operator==()
.
See also insert().
[since 5.6]
QLinkedList::reverse_iterator QLinkedList::rend()Returns a STL-style reverse iterator pointing to one past the last item in the list, in reverse order.
This function was introduced in Qt 5.6.
See also end(), crend(), and rbegin().
[since 5.6]
QLinkedList::const_reverse_iterator QLinkedList::rend()
constThis is an overloaded function.
This function was introduced in Qt 5.6.
Returns the number of items in the list.
See also isEmpty() and count().
Returns true
if the list is not empty and its first item is equal to value; otherwise returns false
.
See also isEmpty() and first().
Swaps list other with this list. This operation is very fast and never fails.
Removes the first item in the list and returns it.
If you don't use the return value, removeFirst() is more efficient.
See also takeLast() and removeFirst().
Removes the last item in the list and returns it.
If you don't use the return value, removeLast() is more efficient.
See also takeFirst() and removeLast().
Returns a std::list object with the data contained in this QLinkedList. Example:
QLinkedList<double> list; list << 1.2 << 0.5 << 3.14; std::list<double> stdlist = list.toStdList();
See also fromStdList().
Returns true
if other is not equal to this list; otherwise returns false
.
Two lists are considered equal if they contain the same values in the same order.
This function requires the value type to implement operator==()
.
See also operator==().
Returns a list that contains all the items in this list followed by all the items in the other list.
See also operator+=().
Appends the items of the other list to this list and returns a reference to this list.
See also operator+() and append().
This is an overloaded function.
Appends value to the list.
Appends the items of the other list to this list and returns a reference to this list.
See also operator+=() and append().
This is an overloaded function.
Appends value to the list.
Returns true
if other is equal to this list; otherwise returns false.
Two lists are considered equal if they contain the same values in the same order.
This function requires the value type to implement operator==()
.
See also operator!=().
Writes the linked list list to stream out.
This function requires the value type to implement operator<<()
.
See also Format of the QDataStream operators.
Reads a linked list from stream in into list.
This function requires the value type to implement operator>>()
.
See also Format of the QDataStream operators.