QLinkedList 类

template <typename T> class QLinkedList

QLinkedList 类是提供链接列表的模板类。 更多...

头: #include <QLinkedList>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core5Compat)
target_link_libraries(mytarget PRIVATE Qt6::Core5Compat)
qmake: QT += core5compat

注意: 此类的所有函数 可重入 .

公共类型

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 (std::initializer_list<T> list )
QLinkedList (InputIterator first , InputIterator last )
QLinkedList (const QLinkedList<T> & other )
QLinkedList (QLinkedList<T> && other )
~QLinkedList ()
void append (const T & value )
T & back ()
const T & back () const
QLinkedList<T>::iterator begin ()
QLinkedList<T>::const_iterator begin () const
QLinkedList<T>::const_iterator cbegin () const
QLinkedList<T>::const_iterator cend () const
void clear ()
QLinkedList<T>::const_iterator constBegin () const
QLinkedList<T>::const_iterator constEnd () const
bool contains (const T & value ) const
int count (const T & value ) const
int count () const
QLinkedList<T>::const_reverse_iterator crbegin () const
QLinkedList<T>::const_reverse_iterator crend () const
bool empty () const
QLinkedList<T>::iterator end ()
QLinkedList<T>::const_iterator end () const
bool endsWith (const T & value ) const
QLinkedList<T>::iterator erase (QLinkedList<T>::iterator pos )
QLinkedList<T>::iterator erase (QLinkedList<T>::iterator begin , QLinkedList<T>::iterator end )
T & first ()
const T & first () const
T & front ()
const T & front () const
QLinkedList<T>::iterator insert (QLinkedList<T>::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<T>::reverse_iterator rbegin ()
QLinkedList<T>::const_reverse_iterator rbegin () const
int removeAll (const T & value )
void removeFirst ()
void removeLast ()
bool removeOne (const T & value )
QLinkedList<T>::reverse_iterator rend ()
QLinkedList<T>::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 )
QLinkedList<T> & operator= (const QLinkedList<T> & other )
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 容器类 . It stores a list of values and provides iterator-based access as well as 常量时间 insertions and removals.

QList <T> and QLinkedList<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. Its items occupy adjacent memory positions. It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of 常量时间 insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.

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

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 () 或 last (). If you want to remove an item from either end of the list, use removeFirst () 或 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 () 和 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 可赋值数据类型 . 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 () 和 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 风格迭代器 ( QLinkedListIterator and QMutableLinkedListIterator ) 和 STL 样式迭代器 ( QLinkedList::const_iterator and QLinkedList::iterator ). See the documentation for these classes for details.

另请参阅 QLinkedListIterator , QMutableLinkedListIterator ,和 QList .

成员类型文档编制

QLinkedList:: ConstIterator

Qt 样式同义词 QLinkedList::const_iterator .

QLinkedList:: Iterator

Qt 样式同义词 QLinkedList::iterator .

QLinkedList:: const_pointer

Typedef for const T *. Provided for STL compatibility.

QLinkedList:: const_reference

Typedef for const T &. Provided for STL compatibility.

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 隐式共享迭代器问题 .

另请参阅 QLinkedList::rbegin (), QLinkedList::rend (), QLinkedList::reverse_iterator ,和 QLinkedList::const_iterator .

QLinkedList:: difference_type

typedef 对于 ptrdiff_t。为兼容 STL 提供。

QLinkedList:: pointer

Typedef for T *. Provided for STL compatibility.

QLinkedList:: reference

Typedef for T &. Provided for STL compatibility.

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 隐式共享迭代器问题 .

另请参阅 QLinkedList::rbegin (), QLinkedList::rend (), QLinkedList::const_reverse_iterator ,和 QLinkedList::iterator .

QLinkedList:: size_type

typedef 对于 int。为兼容 STL 提供。

QLinkedList:: value_type

typedef 对于 T。为兼容 STL 提供。

成员函数文档编制

[noexcept] QLinkedList:: QLinkedList ()

构造空列表。

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.

template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> 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 .

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

构造副本为 other .

This operation occurs in 常量时间 , because QLinkedList is 隐式共享 . 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 线性时间 .

另请参阅 operator= ().

[noexcept] QLinkedList:: QLinkedList ( QLinkedList < T > && other )

Move-constructs a QLinkedList instance, making it point at the same object that other 所指向的。

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 )

插入 value 在列表末尾。

范例:

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 ).

另请参阅 operator<< (), prepend (),和 insert ().

T &QLinkedList:: back ()

此函数为兼容 STL (标准模板库) 提供。它相当于 last ().

const T &QLinkedList:: back () const

这是重载函数。

QLinkedList < T > ::iterator QLinkedList:: begin ()

返回 STL 样式迭代器 指向列表首项。

另请参阅 constBegin () 和 end ().

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: begin () const

这是重载函数。

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: cbegin () const

返回常量 STL 样式迭代器 指向列表首项。

另请参阅 begin () 和 cend ().

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: cend () const

返回常量 STL 样式迭代器 pointing to the imaginary item after the last item in the list.

另请参阅 cbegin () 和 end ().

void QLinkedList:: clear ()

Removes all the items in the list.

另请参阅 removeAll ().

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: constBegin () const

返回常量 STL 样式迭代器 指向列表首项。

另请参阅 begin () 和 constEnd ().

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: constEnd () const

返回常量 STL 样式迭代器 pointing to the imaginary item after the last item in the list.

另请参阅 constBegin () 和 end ().

bool QLinkedList:: contains (const T & value ) const

返回 true if the list contains an occurrence of value ;否则返回 false .

This function requires the value type to have an implementation of operator==() .

另请参阅 QLinkedListIterator::findNext () 和 QLinkedListIterator::findPrevious ().

int QLinkedList:: count (const T & value ) const

Returns the number of occurrences of value 在列表中。

This function requires the value type to have an implementation of operator==() .

另请参阅 contains ().

int QLinkedList:: count () const

如同 size ().

[noexcept] QLinkedList < T > ::const_reverse_iterator QLinkedList:: crbegin () const

返回常量 STL-style reverse iterator pointing to the first item in the list, in reverse order.

另请参阅 begin (), rbegin (),和 rend ().

[noexcept] QLinkedList < T > ::const_reverse_iterator QLinkedList:: crend () const

返回常量 STL-style reverse iterator pointing to one past the last item in the list, in reverse order.

另请参阅 end (), rend (),和 rbegin ().

bool QLinkedList:: empty () const

此函数为兼容 STL (标准模板库) 提供。它相当于 isEmpty () 并返回 true if the list is empty.

QLinkedList < T > ::iterator QLinkedList:: end ()

返回 STL 样式迭代器 pointing to the imaginary item after the last item in the list.

另请参阅 begin () 和 constEnd ().

[noexcept] QLinkedList < T > ::const_iterator QLinkedList:: end () const

这是重载函数。

bool QLinkedList:: endsWith (const T & value ) const

返回 true if the list is not empty and its last item is equal to value ;否则返回 false .

另请参阅 isEmpty () 和 last ().

QLinkedList < T > ::iterator QLinkedList:: erase ( QLinkedList < T > ::iterator pos )

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 ()).

另请参阅 insert ().

QLinkedList < T > ::iterator QLinkedList:: erase ( QLinkedList < T > ::iterator begin , QLinkedList < T > ::iterator end )

这是重载函数。

Removes all the items from begin up to (but not including) end .

T &QLinkedList:: first ()

Returns a reference to the first item in the list. This function assumes that the list isn't empty.

另请参阅 last () 和 isEmpty ().

const T &QLinkedList:: first () const

这是重载函数。

[static] QLinkedList < T > QLinkedList:: fromStdList (const std::list < T > & list )

返回 QLinkedList object with the data contained in list . The order of the elements in the QLinkedList is the same as in list .

范例:

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);
					

另请参阅 toStdList ().

T &QLinkedList:: front ()

此函数为兼容 STL (标准模板库) 提供。它相当于 first ().

const T &QLinkedList:: front () const

这是重载函数。

QLinkedList < T > ::iterator QLinkedList:: insert ( QLinkedList < T > ::iterator before , const T & value )

插入 value in front of the item pointed to by the iterator before . Returns an iterator pointing at the inserted item.

另请参阅 erase ().

bool QLinkedList:: isEmpty () const

返回 true if the list contains no items; otherwise returns false.

另请参阅 size ().

T &QLinkedList:: last ()

Returns a reference to the last item in the list. This function assumes that the list isn't empty.

另请参阅 first () 和 isEmpty ().

const T &QLinkedList:: last () const

这是重载函数。

void QLinkedList:: pop_back ()

此函数为兼容 STL (标准模板库) 提供。它相当于 removeLast ().

void QLinkedList:: pop_front ()

此函数为兼容 STL (标准模板库) 提供。它相当于 removeFirst ().

void QLinkedList:: prepend (const T & value )

插入 value 在列表的开头。

范例:

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 ).

另请参阅 append () 和 insert ().

void QLinkedList:: push_back (const T & value )

This function is provided for STL compatibility. It is equivalent to append( value ).

void QLinkedList:: push_front (const T & value )

This function is provided for STL compatibility. It is equivalent to prepend( value ).

QLinkedList < T > ::reverse_iterator QLinkedList:: rbegin ()

返回 STL-style reverse iterator pointing to the first item in the list, in reverse order.

另请参阅 begin (), crbegin (),和 rend ().

[noexcept] QLinkedList < T > ::const_reverse_iterator QLinkedList:: rbegin () const

这是重载函数。

int QLinkedList:: removeAll (const T & value )

Removes all occurrences of value 在列表中。

范例:

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==() .

另请参阅 insert ().

void QLinkedList:: removeFirst ()

Removes the first item in the list.

This is the same as erase( begin ()).

另请参阅 removeLast () 和 erase ().

void QLinkedList:: removeLast ()

Removes the last item in the list.

另请参阅 removeFirst () 和 erase ().

bool QLinkedList:: removeOne (const T & value )

Removes the first occurrences of value in the list. Returns true 当成功时;否则返回 false .

范例:

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==() .

另请参阅 insert ().

QLinkedList < T > ::reverse_iterator QLinkedList:: rend ()

返回 STL-style reverse iterator pointing to one past the last item in the list, in reverse order.

另请参阅 end (), crend (),和 rbegin ().

[noexcept] QLinkedList < T > ::const_reverse_iterator QLinkedList:: rend () const

这是重载函数。

int QLinkedList:: size () const

Returns the number of items in the list.

另请参阅 isEmpty () 和 count ().

bool QLinkedList:: startsWith (const T & value ) const

返回 true if the list is not empty and its first item is equal to value ;否则返回 false .

另请参阅 isEmpty () 和 first ().

[noexcept] void QLinkedList:: swap ( QLinkedList < T > & other )

Swaps list other with this list. This operation is very fast and never fails.

T QLinkedList:: takeFirst ()

Removes the first item in the list and returns it.

若不使用返回值, removeFirst () 效率更高。

另请参阅 takeLast () 和 removeFirst ().

T QLinkedList:: takeLast ()

Removes the last item in the list and returns it.

若不使用返回值, removeLast () 效率更高。

另请参阅 takeFirst () 和 removeLast ().

std::list < T > QLinkedList:: toStdList () const

Returns a std::list object with the data contained in this QLinkedList 。范例:

QLinkedList<double> list;
list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();
					

另请参阅 fromStdList ().

bool QLinkedList:: operator!= (const QLinkedList < T > & other ) const

返回 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==() .

另请参阅 operator== ().

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

Returns a list that contains all the items in this list followed by all the items in the other 列表。

另请参阅 operator+= ().

QLinkedList < T > &QLinkedList:: operator+= (const QLinkedList < T > & other )

Appends the items of the other list to this list and returns a reference to this list.

另请参阅 operator+ () 和 append ().

QLinkedList < T > &QLinkedList:: operator+= (const T & value )

这是重载函数。

追加 value to the list.

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

Appends the items of the other list to this list and returns a reference to this list.

另请参阅 operator+= () 和 append ().

QLinkedList < T > &QLinkedList:: operator<< (const T & value )

这是重载函数。

追加 value to the list.

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

赋值 other to this list and returns a reference to this list.

bool QLinkedList:: operator== (const QLinkedList < T > & other ) const

返回 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==() .

另请参阅 operator!= ().

相关非成员

template <typename T> QDataStreamIfHasOStreamOperatorsContainer < QLinkedList < T >, T > operator<< ( QDataStream & out , const QLinkedList < T > & list )

Writes the linked list list 到流 out .

This function requires the value type to implement operator<<() .

另请参阅 QDataStream 运算符格式 .

template <typename T> QDataStreamIfHasIStreamOperatorsContainer < QLinkedList < T >, T > operator>> ( QDataStream & in , QLinkedList < T > & list )

Reads a linked list from stream in into list .

This function requires the value type to implement operator>>() .

另请参阅 QDataStream 运算符格式 .