QList 類

template <typename T> class QList

QList 類是提供動態數組的模闆類。 更多...

頭: #include <QList>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
繼承者:
16 types

QBluetoothServiceInfo::Alternative , QBluetoothServiceInfo::Sequence , QByteArrayList , QItemSelection , QMqttUserProperties , QNdefMessage , QPolygon , QPolygonF , QQueue , QSignalSpy , QStack , QStringList , QTestEventList , QVector , QVulkanInfoVector ,和 QXmlStreamAttributes

注意: 此類的所有函數 可重入 .

公共類型

class const_iterator
class iterator
ConstIterator
Iterator
const_pointer
const_reference
const_reverse_iterator
difference_type
parameter_type
pointer
reference
reverse_iterator
rvalue_ref
size_type
value_type

公共函數

QList ()
QList (qsizetype size )
QList (std::initializer_list<T> args )
QList (InputIterator first , InputIterator last )
QList (qsizetype size , QList<T>::parameter_type value )
(從 6.8 起) QList (qsizetype size , Qt::Initialization)
QList (const QList<T> & other )
QList (QList<T> && other )
~QList ()
void append (QList<T>::parameter_type value )
(從 6.0 起) void append (QList<T> && value )
void append (QList<T>::rvalue_ref value )
void append (const QList<T> & value )
(從 6.6 起) QList<T> & assign (std::initializer_list<T> l )
(從 6.6 起) QList<T> & assign (InputIterator first , InputIterator last )
(從 6.6 起) QList<T> & assign (qsizetype n , QList<T>::parameter_type t )
QList<T>::const_reference at (qsizetype i ) const
QList<T>::reference back ()
QList<T>::const_reference back () const
QList<T>::iterator begin ()
QList<T>::const_iterator begin () const
qsizetype capacity () const
QList<T>::const_iterator cbegin () const
QList<T>::const_iterator cend () const
void clear ()
QList<T>::const_iterator constBegin () const
QList<T>::const_pointer constData () const
QList<T>::const_iterator constEnd () const
const T & constFirst () const
const T & constLast () const
bool contains (const AT & value ) const
qsizetype count (const AT & value ) const
qsizetype count () const
QList<T>::const_reverse_iterator crbegin () const
QList<T>::const_reverse_iterator crend () const
QList<T>::pointer data ()
QList<T>::const_pointer data () const
QList<T>::iterator emplace (qsizetype i , Args &&... args )
QList<T>::iterator emplace (QList<T>::const_iterator before , Args &&... args )
QList<T>::reference emplaceBack (Args &&... args )
QList<T>::reference emplace_back (Args &&... args )
bool empty () const
QList<T>::iterator end ()
QList<T>::const_iterator end () const
bool endsWith (QList<T>::parameter_type value ) const
QList<T>::iterator erase (QList<T>::const_iterator pos )
QList<T>::iterator erase (QList<T>::const_iterator begin , QList<T>::const_iterator end )
QList<T> & fill (QList<T>::parameter_type value , qsizetype size = -1)
T & first ()
(從 6.0 起) QList<T> first (qsizetype n ) const
const T & first () const
QList<T>::reference front ()
QList<T>::const_reference front () const
qsizetype indexOf (const AT & value , qsizetype from = 0) const
QList<T>::iterator insert (qsizetype i , QList<T>::parameter_type value )
QList<T>::iterator insert (qsizetype i , QList<T>::rvalue_ref value )
QList<T>::iterator insert (QList<T>::const_iterator before , qsizetype count , QList<T>::parameter_type value )
QList<T>::iterator insert (QList<T>::const_iterator before , QList<T>::parameter_type value )
QList<T>::iterator insert (QList<T>::const_iterator before , QList<T>::rvalue_ref value )
QList<T>::iterator insert (qsizetype i , qsizetype count , QList<T>::parameter_type value )
bool isEmpty () const
T & last ()
(從 6.0 起) QList<T> last (qsizetype n ) const
const T & last () const
qsizetype lastIndexOf (const AT & value , qsizetype from = -1) const
qsizetype length () const
(從 6.8 起) qsizetype max_size () const
QList<T> mid (qsizetype pos , qsizetype length = -1) const
void move (qsizetype from , qsizetype to )
void pop_back ()
void pop_front ()
void prepend (QList<T>::parameter_type value )
void prepend (QList<T>::rvalue_ref value )
void push_back (QList<T>::parameter_type value )
void push_back (QList<T>::rvalue_ref value )
void push_front (QList<T>::parameter_type value )
void push_front (QList<T>::rvalue_ref value )
QList<T>::reverse_iterator rbegin ()
QList<T>::const_reverse_iterator rbegin () const
void remove (qsizetype i , qsizetype n = 1)
qsizetype removeAll (const AT & t )
void removeAt (qsizetype i )
void removeFirst ()
(從 6.1 起) qsizetype removeIf (Predicate pred )
void removeLast ()
bool removeOne (const AT & t )
QList<T>::reverse_iterator rend ()
QList<T>::const_reverse_iterator rend () const
void replace (qsizetype i , QList<T>::parameter_type value )
void replace (qsizetype i , QList<T>::rvalue_ref value )
void reserve (qsizetype size )
(從 6.0 起) void resize (qsizetype size )
(從 6.0 起) void resize (qsizetype size , QList<T>::parameter_type c )
(從 6.8 起) void resizeForOverwrite (qsizetype size )
void shrink_to_fit ()
qsizetype size () const
(從 6.0 起) QList<T> sliced (qsizetype pos , qsizetype n ) const
(從 6.0 起) QList<T> sliced (qsizetype pos ) const
void squeeze ()
bool startsWith (QList<T>::parameter_type value ) const
void swap (QList<T> & other )
void swapItemsAt (qsizetype i , qsizetype j )
T takeAt (qsizetype i )
QList<T>::value_type takeFirst ()
QList<T>::value_type takeLast ()
T value (qsizetype i ) const
T value (qsizetype i , QList<T>::parameter_type defaultValue ) const
bool operator!= (const QList<T> & other ) const
QList<T> operator+ (QList<T> && other ) &&
QList<T> operator+ (const QList<T> & other ) &&
QList<T> operator+ (QList<T> && other ) const &
QList<T> operator+ (const QList<T> & other ) const &
QList<T> & operator+= (const QList<T> & other )
(從 6.0 起) QList<T> & operator+= (QList<T> && other )
QList<T> & operator+= (QList<T>::parameter_type value )
QList<T> & operator+= (QList<T>::rvalue_ref value )
bool operator< (const QList<T> & other ) const
QList<T> & operator<< (QList<T>::parameter_type value )
QList<T> & operator<< (const QList<T> & other )
(從 6.0 起) QList<T> & operator<< (QList<T> && other )
QList<T> & operator<< (QList<T>::rvalue_ref value )
bool operator<= (const QList<T> & other ) const
QList<T> & operator= (QList<T> && other )
QList<T> & operator= (const QList<T> & other )
QList<T> & operator= (std::initializer_list<T> args )
bool operator== (const QList<T> & other ) const
bool operator> (const QList<T> & other ) const
bool operator>= (const QList<T> & other ) const
QList<T>::reference operator[] (qsizetype i )
QList<T>::const_reference operator[] (qsizetype i ) const

靜態公共成員

(從 6.8 起) qsizetype maxSize ()
(從 6.1 起) qsizetype erase (QList<T> & list , const AT & t )
(從 6.1 起) qsizetype erase_if (QList<T> & list , Predicate pred )
size_t qHash (const QList<T> & key , size_t seed = 0)
QDataStream & operator<< (QDataStream & out , const QList<T> & list )
(從 6.9 起) auto operator<=> (const QList<T> & lhs , const QList<T> & rhs )
QDataStream & operator>> (QDataStream & in , QList<T> & list )

詳細描述

QList 是一種 Qt 一般 容器類 。它將項存儲在相鄰內存位置,並提供基於索引的快速訪問。 QVector <T> 在 Qt 5 中用於不同類,但現在是簡單 QList 彆名。

QList<T> 和 QVarLengthArray <T> 提供類似 API 和功能。它們經常可互換,但有性能後果。這裏是用例概述:

  • QList 應該是您的默認第一抉擇。
  • QVarLengthArray provides an array that reserves space on the stack, but can dynamically grow onto the heap if required. It's good to use for short lived containers that are usually small.
  • If you need a real linked list, which guarantees 常量時間 insertions mid-list and uses iterators to items rather than indexes, use std::list.

注意: QList 和 QVarLengthArray 兩者均保證兼容 C 數組布局。

注意: QList in Qt 5 did not always have a C-compatible array layout and we often recommended to use QVector instead for more predictable performance. This is not the case in Qt 6 anymore, where both classes now share an implementation and can be used interchangeably.

Here's an example of a QList that stores integers and a QList that stores QString 值:

QList<int> integerList;
QList<QString> stringList;
					

QList stores its items in an array of continuous memory. Typically, lists are created with an initial size. For example, the following code constructs a QList with 200 elements:

QList<QString> list(200);
					

The elements are automatically initialized with a 默認構造值 . If you want to initialize the list with a different value, pass that value as the second argument to the constructor:

QList<QString> list(200, "Pass");
					

You can also call fill () at any time to fill the list with a value.

QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const lists, operator[]() returns a reference to the item that can be used on the left side of an assignment:

if (list[0] == "Liz")
    list[0] = "Elizabeth";
					

For read-only access, an alternative syntax is to use at ():

for (qsizetype i = 0; i < list.size(); ++i) {
    if (list.at(i) == "Alfonso")
        cout << "Found Alfonso at position " << i << endl;
}
					

at () can be faster than operator[](), because it never causes a 深拷貝 的齣現。

Another way to access the data stored in a QList is to call data (). The function returns a pointer to the first item in the list. You can use the pointer to directly access and modify the elements stored in the list. The pointer is also useful if you need to pass a QList to a function that accepts a plain C++ array.

If you want to find all occurrences of a particular value in a list, use indexOf () 或 lastIndexOf (). The former searches forward starting from a given index position, the latter searches backward. Both return the index of the matching item if they found one; otherwise, they return -1. For example:

qsizetype i = list.indexOf("Harumi");
if (i != -1)
    cout << "First occurrence of Harumi is at position " << i << endl;
					

If you simply want to check whether a list contains a particular value, use contains (). If you want to find out how many times a particular value occurs in the list, use count ().

QList provides these basic functions to add, move, and remove items: insert (), replace (), remove (), prepend (), append (). With the exception of append (), prepend () 和 replace (), these functions can be slow ( 綫性時間 ) for large lists, because they require moving many items in the list by one position in memory. If you want a container class that provides fast insertion/removal in the middle, use std::list instead.

Unlike plain C++ arrays, QLists can be resized at any time by calling resize (). If the new size is larger than the old size, QList might need to reallocate the whole list. QList tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs.

If you're building a QList gradually and know in advance approximately how many elements it will contain, you can call reserve (), asking QList to preallocate a certain amount of memory. You can also call capacity () to find out how much memory the QList actually has allocated.

Note that using non-const operators and functions can cause QList to do a deep copy of the data, due to 隱式共享 .

QList'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 作為值;取而代之,存儲 QWidget *. A few functions have additional requirements; for example, indexOf () 和 lastIndexOf () expect the value type to support operator==() . These requirements are documented on a per-function basis.

For iterating over the items, see 遍曆容器 . For using QList with functions from <algorithm> header, such as std::sort() , std::reverse() ,和 std::count_if() ,見 Qt 容器和 std 算法 .

除 QList 外,Qt 還提供 QVarLengthArray ,具有小的功能速度優化的很低級類。

有關使用 Qt 容器的更多信息

有關比較 Qt 各種容器和 STL 容器的詳細討論,見 理解 Qt 容器 .

最大尺寸和內存不足情況

The maximum size of QList depends on the architecture. Most 64-bit systems can allocate more than 2 GB of memory, with a typical limit of 2^63 bytes. The actual value also depends on the overhead required for managing the data block. As a result, you can expect the maximum size of 2 GB minus overhead on 32-bit platforms, and 2^63 bytes minus overhead on 64-bit platforms. The number of elements that can be stored in a QList is this maximum size divided by the size of a stored element.

When memory allocation fails, QList uses the Q_CHECK_PTR macro, which throws a std::bad_alloc exception if the application is being compiled with exception support. If exceptions are disabled, then running out of memory is undefined behavior.

Note that the operating system may impose further limits on applications holding a lot of allocated memory, especially large, contiguous blocks. Such considerations, the configuration of such behavior or any mitigation are outside the scope of the Qt API.

成員類型文檔編製

[alias] QList:: ConstIterator

Qt 樣式同義詞 QList::const_iterator .

[alias] QList:: Iterator

Qt 樣式同義詞 QList::iterator .

[alias] QList:: const_pointer

為兼容 STL 提供。

[alias] QList:: const_reference

為兼容 STL 提供。

[alias] QList:: const_reverse_iterator

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

警告: 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 隱式共享迭代器問題 .

警告: Iterators are invalidated when QList is modified. Consider that all iterators are invalidated by default. Exceptions to this rule are explicitly documented.

另請參閱 QList::rbegin (), QList::rend (), QList::reverse_iterator ,和 QList::const_iterator .

[alias] QList:: difference_type

為兼容 STL 提供。

[alias] QList:: parameter_type

[alias] QList:: pointer

為兼容 STL 提供。

[alias] QList:: reference

為兼容 STL 提供。

[alias] QList:: reverse_iterator

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

警告: 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 隱式共享迭代器問題 .

警告: Iterators are invalidated when QList is modified. Consider that all iterators are invalidated by default. Exceptions to this rule are explicitly documented.

另請參閱 QList::rbegin (), QList::rend (), QList::const_reverse_iterator ,和 QList::iterator .

[alias] QList:: rvalue_ref

[alias] QList:: size_type

為兼容 STL 提供。

[alias] QList:: value_type

為兼容 STL 提供。

成員函數文檔編製

[since 6.0] void QList:: resize ( qsizetype size )

[since 6.0] void QList:: resize ( qsizetype size , QList < T > ::parameter_type c )

Sets the size of the list to size 。若 size is greater than the current size, elements are added to the end; the new elements are initialized with either a 默認構造值 or c 。若 size is less than the current size, elements are removed from the end.

If this list is not shared, the capacity () is preserved. Use squeeze () to shed excess capacity.

注意: In Qt versions prior to 5.7 (for QVector ; QList lacked a resize () until 6.0), this function released the memory used by the list instead of preserving the capacity.

這些函數在 Qt 6.0 引入。

另請參閱 size ().

void QList:: prepend ( QList < T > ::parameter_type value )

void QList:: prepend ( QList < T > ::rvalue_ref value )

插入 value 在列錶的開頭。

範例:

QList<QString> list;
list.prepend("one");
list.prepend("two");
list.prepend("three");
// list: ["three", "two", "one"]
					

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

Normally this operation is relatively fast (amortized 常量時間 ). QList is able to allocate extra memory at the beginning of the list data and grow in that direction without reallocating or moving the data on each operation. However if you want a container class with a guarantee of 常量時間 prepend, use std::list instead, but prefer QList 否則。

另請參閱 append () 和 insert ().

template <typename... Args> QList < T > ::reference QList:: emplaceBack ( Args &&... args )

template <typename... Args> QList < T > ::reference QList:: emplace_back ( Args &&... args )

Adds a new element to the end for the container. This new element is constructed in-place using args as the arguments for its construction.

返迴對新元素的引用。

範例:

QList<QString> list{"one", "two"};
list.emplaceBack(3, 'a');
qDebug() << list;
// list: ["one", "two", "aaa"]
					

It is also possible to access a newly created object by using returned reference:

QList<QString> list;
auto &ref = list.emplaceBack();
ref = "one";
// list: ["one"]
					

This is the same as list.emplace(list. size (), args ).

另請參閱 emplace .

QList < T > ::iterator QList:: insert ( qsizetype i , QList < T > ::parameter_type value )

QList < T > ::iterator QList:: insert ( qsizetype i , QList < T > ::rvalue_ref value )

插入 value at index position i in the list. If i is 0, the value is prepended to the list. If i is size (), the value is appended to the list.

範例:

QList<QString> list = {"alpha", "beta", "delta"};
list.insert(2, "gamma");
// list: ["alpha", "beta", "gamma", "delta"]
					

For large lists, this operation can be slow ( 綫性時間 ), because it requires moving all the items at indexes i and above by one position further in memory. If you want a container class that provides a fast insert () function, use std::list instead.

另請參閱 append (), prepend (),和 remove ().

QList < T > ::iterator QList:: insert ( QList < T > ::const_iterator before , QList < T > ::parameter_type value )

QList < T > ::iterator QList:: insert ( QList < T > ::const_iterator before , QList < T > ::rvalue_ref value )

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

void QList:: replace ( qsizetype i , QList < T > ::parameter_type value )

void QList:: replace ( qsizetype i , QList < T > ::rvalue_ref value )

替換項在索引位置 i with value .

i must be a valid index position in the list (i.e., 0 <= i < size ()).

另請參閱 operator[] () 和 remove ().

void QList:: push_front ( QList < T > ::parameter_type value )

void QList:: push_front ( QList < T > ::rvalue_ref value )

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

[static constexpr, since 6.8] qsizetype QList:: maxSize ()

[constexpr noexcept, since 6.8] qsizetype QList:: max_size () const

It returns the maximum number of elements that the list can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system.

這些函數在 Qt 6.8 引入。

QList < T > QList:: operator+ ( QList < T > && other ) &&

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

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

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

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

另請參閱 operator+= ().

[constexpr noexcept] QList:: QList ()

構造空列錶。

另請參閱 resize ().

[explicit] QList:: QList ( qsizetype size )

構造列錶采用初始尺寸為 size 元素。

元素被初始化采用 默認構造值 .

另請參閱 resize ().

QList:: QList ( std::initializer_list < T > args )

Constructs a list from the std::initializer_list given by args .

template <typename InputIterator, QList<T>::if_input_iterator<InputIterator> = true> QList:: QList ( 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 .

約束

Participates in overload resolution only if InputIterator meets the requirements of a LegacyInputIterator .

QList:: QList ( qsizetype size , QList < T > ::parameter_type value )

構造列錶采用初始尺寸為 size elements. Each element is initialized with value .

另請參閱 resize () 和 fill ().

[since 6.8] QList:: QList ( qsizetype size , Qt::Initialization )

構造列錶采用初始尺寸為 size 元素。

QList will make an attempt at not initializing the elements.

Specifically:

  • if T has a constructor that accepts Qt::Uninitialized , that constructor will be used to initialize the elements;
  • otherwise, each element is default constructed. For trivially constructible types (such as int , float , etc.) this is equivalent to not initializing them.

該函數在 Qt 6.8 引入。

另請參閱 resizeForOverwrite ().

[default] QList:: QList (const QList < T > & other )

構造副本為 other .

此操作花費 常量時間 , because QList is 隱式共享 . This makes returning a QList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes 綫性時間 .

另請參閱 operator= ().

[default] QList:: QList ( QList < T > && other )

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

[default] QList:: ~QList ()

Destroys the list.

void QList:: append ( QList < T > ::parameter_type value )

插入 value 在列錶末尾。

範例:

QList<QString> list;
list.append("one");
list.append("two");
QString three = "three";
list.append(three);
// list: ["one", "two", "three"]
// three: "three"
					

This is the same as calling resize( size () + 1) and assigning value to the new last element in the list.

This operation is relatively fast, because QList typically allocates more memory than necessary, so it can grow without reallocating the entire list each time.

另請參閱 operator<< (), prepend (),和 insert ().

[since 6.0] void QList:: append ( QList < T > && value )

Moves the items of the value list to the end of this list.

這是重載函數。

該函數在 Qt 6.0 引入。

另請參閱 operator<< () 和 operator+= ().

void QList:: append ( QList < T > ::rvalue_ref value )

範例:

QList<QString> list;
list.append("one");
list.append("two");
QString three = "three";
list.append(std::move(three));
// list: ["one", "two", "three"]
// three: ""
					

這是重載函數。

void QList:: append (const QList < T > & value )

Appends the items of the value list to this list.

這是重載函數。

另請參閱 operator<< () 和 operator+= ().

[since 6.6] QList < T > &QList:: assign ( std::initializer_list < T > l )

Replaces the contents of this list with a copy of the elements of l .

The size of this list will be equal to the number of elements in l .

This function only allocates memory if the number of elements in l exceeds the capacity of this list or this list is shared.

該函數在 Qt 6.6 引入。

[since 6.6] template <typename InputIterator, QList<T>::if_input_iterator<InputIterator> = true> QList < T > &QList:: assign ( InputIterator first , InputIterator last )

Replaces the contents of this list with a copy of the elements in the iterator range [ first , last ).

The size of this list will be equal to the number of elements in the range [ first , last ).

This function will only allocate memory if the number of elements in the range exceeds the capacity of this list or this list is shared.

注意: The behavior is undefined if either argument is an iterator into *this.

約束

Participates in overload resolution only if InputIterator meets the requirements of a LegacyInputIterator .

該函數在 Qt 6.6 引入。

[since 6.6] QList < T > &QList:: assign ( qsizetype n , QList < T > ::parameter_type t )

Replaces the contents of this list with n 個副本對於 t .

The size of this list will be equal to n .

此函數將隻分配內存若 n exceeds the capacity of the list or this list is shared.

該函數在 Qt 6.6 引入。

[noexcept] QList < T > ::const_reference QList:: at ( qsizetype i ) const

返迴項按索引位置 i 在列錶中。

i must be a valid index position in the list (i.e., 0 <= i < size ()).

另請參閱 value () 和 operator[] ().

QList < T > ::reference QList:: back ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 last ().

[noexcept] QList < T > ::const_reference QList:: back () const

這是重載函數。

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

返迴 STL 樣式迭代器 指嚮列錶首項。

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 constBegin () 和 end ().

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

這是重載函數。

qsizetype QList:: capacity () const

Returns the maximum number of items that can be stored in the list without forcing a reallocation.

The sole purpose of this function is to provide a means of fine tuning QList 's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the list, call size ().

注意: a statically allocated list will report a capacity of 0, even if it's not empty.

警告: The free space position in the allocated memory block is undefined. In other words, you should not assume that the free memory is always located at the end of the list. You can call reserve () to ensure that there is enough space at the end.

另請參閱 reserve () 和 squeeze ().

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

返迴常量 STL 樣式迭代器 指嚮列錶首項。

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 begin () 和 cend ().

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

返迴常量 STL 樣式迭代器 pointing just after the last item in the list.

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 cbegin () 和 end ().

void QList:: clear ()

Removes all the elements from the list.

If this list is not shared, the capacity () is preserved. Use squeeze () to shed excess capacity.

注意: In Qt versions prior to 5.7 (for QVector ) and 6.0 (for QList ), this function released the memory used by the list instead of preserving the capacity.

另請參閱 resize () 和 squeeze ().

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

返迴常量 STL 樣式迭代器 指嚮列錶首項。

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 begin () 和 constEnd ().

[noexcept] QList < T > ::const_pointer QList:: constData () const

Returns a const pointer to the data stored in the list. The pointer can be used to access the items in the list.

警告: The pointer is invalidated on detachment or when the QList 被修改。

This function is mostly useful to pass a list to a function that accepts a plain C++ array.

另請參閱 data () 和 operator[] ().

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

返迴常量 STL 樣式迭代器 pointing just after the last item in the list.

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 constBegin () 和 end ().

[noexcept] const T &QList:: constFirst () const

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

另請參閱 constLast (), isEmpty (),和 first ().

[noexcept] const T &QList:: constLast () const

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

另請參閱 constFirst (), isEmpty (),和 last ().

[noexcept] template <typename AT> bool QList:: contains (const AT & value ) const

返迴 true if the list contains an occurrence of value ;否則返迴 false .

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

另請參閱 indexOf () 和 count ().

[noexcept] template <typename AT = T> qsizetype QList:: count (const AT & value ) const

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

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

另請參閱 contains () 和 indexOf ().

[constexpr noexcept] qsizetype QList:: count () const

如同 size ().

這是重載函數。

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

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

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 begin (), rbegin (),和 rend ().

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

返迴常量 STL-style reverse iterator pointing just after the last item in the list, in reverse order.

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 end (), rend (),和 rbegin ().

QList < T > ::pointer QList:: data ()

Returns a pointer to the data stored in the list. The pointer can be used to access and modify the items in the list.

範例:

QList<int> list(10);
int *data = list.data();
for (qsizetype i = 0; i < 10; ++i)
    data[i] = 2 * i;
					

警告: The pointer is invalidated on detachment or when the QList 被修改。

This function is mostly useful to pass a list to a function that accepts a plain C++ array.

另請參閱 constData () 和 operator[] ().

[noexcept] QList < T > ::const_pointer QList:: data () const

這是重載函數。

template <typename... Args> QList < T > ::iterator QList:: emplace ( qsizetype i , Args &&... args )

Extends the container by inserting a new element at position i . This new element is constructed in-place using args as the arguments for its construction.

Returns an iterator to the new element.

範例:

QList<QString> list{"a", "ccc"};
list.emplace(1, 2, 'b');
// list: ["a", "bb", "ccc"]
					

注意: It is guaranteed that the element will be created in place at the beginning, but after that it might be copied or moved to the right position.

另請參閱 emplaceBack .

template <typename... Args> QList < T > ::iterator QList:: emplace ( QList < T > ::const_iterator before , Args &&... args )

Creates a new element in front of the item pointed to by the iterator before . This new element is constructed in-place using args as the arguments for its construction.

Returns an iterator to the new element.

這是重載函數。

[noexcept] bool QList:: empty () const

此函數為兼容 STL (標準模闆庫) 提供。它相當於 isEmpty (), returning true if the list is empty; otherwise returns false .

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

返迴 STL 樣式迭代器 pointing just after the last item in the list.

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 begin () 和 constEnd ().

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

這是重載函數。

bool QList:: endsWith ( QList < T > ::parameter_type value ) const

返迴 true if this list is not empty and its last item is equal to value ;否則返迴 false .

另請參閱 isEmpty () 和 last ().

QList < T > ::iterator QList:: erase ( QList < T > ::const_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 ()).

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

注意: QList 不是 隱式共享 , this function only invalidates iterators at or after the specified position.

另請參閱 insert () 和 remove ().

QList < T > ::iterator QList:: erase ( QList < T > ::const_iterator begin , QList < T > ::const_iterator end )

Removes all the items from begin up to (but not including) end . Returns an iterator to the same item that end referred to before the call.

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

注意: QList 不是 隱式共享 , this function only invalidates iterators at or after the specified position.

這是重載函數。

QList < T > &QList:: fill ( QList < T > ::parameter_type value , qsizetype size = -1)

賦值 value to all items in the list. If size is different from -1 (the default), the list is resized to size 事先。

範例:

QList<QString> list(3);
list.fill("Yes");
// list: ["Yes", "Yes", "Yes"]
list.fill("oh", 5);
// list: ["oh", "oh", "oh", "oh", "oh"]
					

另請參閱 resize ().

T &QList:: 第一 ()

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

另請參閱 last (), isEmpty (),和 constFirst ().

[since 6.0] QList < T > QList:: 第一 ( qsizetype n ) const

Returns a sub-list that contains the first n elements of this list.

注意: 行為未定義當 n < 0 or n > size ().

該函數在 Qt 6.0 引入。

另請參閱 last () 和 sliced ().

[noexcept] const T &QList:: 第一 () const

這是重載函數。

QList < T > ::reference QList:: front ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 first ().

[noexcept] QList < T > ::const_reference QList:: front () const

這是重載函數。

[noexcept] template <typename AT> qsizetype QList:: indexOf (const AT & value , qsizetype from = 0) const

Returns the index position of the first occurrence of value in the list, searching forward from index position from . Returns -1 if no item matched.

範例:

QList<QString> list{"A", "B", "C", "B", "A"};
list.indexOf("B");            // returns 1
list.indexOf("B", 1);         // returns 1
list.indexOf("B", 2);         // returns 3
list.indexOf("X");            // returns -1
					

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

另請參閱 lastIndexOf () 和 contains ().

QList < T > ::iterator QList:: insert ( QList < T > ::const_iterator before , qsizetype count , QList < T > ::parameter_type value )

插入 count 個副本對於 value in front of the item pointed to by the iterator before . Returns an iterator pointing at the first of the inserted items.

QList < T > ::iterator QList:: insert ( qsizetype i , qsizetype count , QList < T > ::parameter_type value )

插入 count 個副本對於 value at index position i 在列錶中。

範例:

QList<double> list = {2.718, 1.442, 0.4342};
list.insert(1, 3, 9.9);
// list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
					

這是重載函數。

[constexpr noexcept] bool QList:: isEmpty () const

返迴 true if the list has size 0; otherwise returns false .

另請參閱 size () 和 resize ().

T &QList:: last ()

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

另請參閱 first (), isEmpty (),和 constLast ().

[since 6.0] QList < T > QList:: last ( qsizetype n ) const

Returns a sub-list that contains the last n elements of this list.

注意: 行為未定義當 n < 0 or n > size ().

該函數在 Qt 6.0 引入。

另請參閱 first () 和 sliced ().

[noexcept] const T &QList:: last () const

這是重載函數。

[noexcept] template <typename AT> qsizetype QList:: lastIndexOf (const AT & value , qsizetype from = -1) const

Returns the index position of the last occurrence of the value value in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

範例:

QList<QString> list = {"A", "B", "C", "B", "A"};
list.lastIndexOf("B");        // returns 3
list.lastIndexOf("B", 3);     // returns 3
list.lastIndexOf("B", 2);     // returns 1
list.lastIndexOf("X");        // returns -1
					

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

另請參閱 indexOf ().

[constexpr noexcept] qsizetype QList:: length () const

如同 size () 和 count ().

另請參閱 size () 和 count ().

QList < T > QList:: mid ( qsizetype pos , qsizetype length = -1) const

Returns a sub-list which contains elements from this list, starting at position pos 。若 length is -1 (the default), all elements after pos are included; otherwise length elements (or all remaining elements if there are less than length elements) are included.

void QList:: move ( qsizetype from , qsizetype to )

移動項按索引位置 from 到索引位置 to .

from and to must be within bounds.

For example, to move the first item to the end of the list:

QList<int> list = {1, 2, 3};
list.move(0, list.size() - 1);
qDebug() << list; // Prints "QList(2, 3, 1)"
					

[noexcept] void QList:: pop_back ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 removeLast ().

[noexcept] void QList:: pop_front ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 removeFirst ().

void QList:: push_back ( QList < T > ::parameter_type value )

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

void QList:: push_back ( QList < T > ::rvalue_ref value )

這是重載函數。

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

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

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 begin (), crbegin (),和 rend ().

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

這是重載函數。

void QList:: remove ( qsizetype i , qsizetype n = 1)

移除 n elements from the list, starting at index position i .

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

注意: QList 不是 隱式共享 , this function only invalidates iterators at or after the specified position.

另請參閱 insert (), replace (),和 fill ().

template <typename AT = T> qsizetype QList:: removeAll (const AT & t )

Removes all elements that compare equal to t from the list. Returns the number of elements removed, if any.

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

另請參閱 removeOne ().

void QList:: removeAt ( qsizetype i )

Removes the element at index position i . Equivalent to

remove(i);
					

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

注意: QList 不是 隱式共享 , this function only invalidates iterators at or after the specified position.

另請參閱 remove ().

[noexcept] void QList:: removeFirst ()

Removes the first item in the list. Calling this function is equivalent to calling remove(0). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

另請參閱 remove (), takeFirst (),和 isEmpty ().

[since 6.1] template <typename Predicate> qsizetype QList:: removeIf ( Predicate pred )

Removes all elements for which the predicate pred returns true from the list. Returns the number of elements removed, if any.

該函數在 Qt 6.1 引入。

另請參閱 removeAll ().

[noexcept] void QList:: removeLast ()

Removes the last item in the list. Calling this function is equivalent to calling remove( size () - 1). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

另請參閱 remove (), takeLast (), removeFirst (),和 isEmpty ().

template <typename AT = T> bool QList:: removeOne (const AT & t )

Removes the first element that compares equal to t from the list. Returns whether an element was, in fact, removed.

Element removal will preserve the list's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze ().

另請參閱 removeAll ().

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

返迴 STL-style reverse iterator pointing just after the last item in the list, in reverse order.

警告: 返迴迭代器不驗證當分離時或當 QList 被修改。

另請參閱 end (), crend (),和 rbegin ().

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

這是重載函數。

void QList:: reserve ( qsizetype size )

試圖分配內存為至少 size 元素。

If you know in advance how large the list will be, you should call this function to prevent reallocations and memory fragmentation. If you resize the list often, you are also likely to get better performance.

If in doubt about how much space shall be needed, it is usually better to use an upper bound as size , or a high estimate of the most likely size, if a strict upper bound would be much bigger than this. If size is an underestimate, the list will grow as needed once the reserved size is exceeded, which may lead to a larger allocation than your best overestimate would have and will slow the operation that triggers it.

警告: reserve() reserves memory but does not change the size of the list. Accessing data beyond the current end of the list is undefined behavior. If you need to access memory beyond the current end of the list, use resize ().

另請參閱 squeeze (), capacity (),和 resize ().

[since 6.8] void QList:: resizeForOverwrite ( qsizetype size )

Sets the size of the list to size 。若 size is less than the current size, elements are removed from the end. If size is greater than the current size, elements are added to the end; QList will make an attempt at not initializing these new elements.

Specifically:

  • if T has a constructor that accepts Qt::Uninitialized , that constructor will be used to initialize the elements;
  • otherwise, each element is default constructed. For trivially constructible types (such as int , float , etc.) this is equivalent to not initializing them.

該函數在 Qt 6.8 引入。

void QList:: shrink_to_fit ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 squeeze ().

[constexpr noexcept] qsizetype QList:: size () const

Returns the number of items in the list.

另請參閱 isEmpty () 和 resize ().

[since 6.0] QList < T > QList:: sliced ( qsizetype pos , qsizetype n ) const

Returns a sub-list that contains n elements of this list, starting at position pos .

注意: 行為未定義當 pos < 0, n < 0, or pos + n > size ().

該函數在 Qt 6.0 引入。

另請參閱 first () 和 last ().

[since 6.0] QList < T > QList:: sliced ( qsizetype pos ) const

Returns a sub-list that contains the elements of this list starting at position pos and extending to its end.

注意: 行為未定義當 pos < 0 or pos > size ().

這是重載函數。

該函數在 Qt 6.0 引入。

另請參閱 first () 和 last ().

void QList:: squeeze ()

Releases any memory not required to store the items.

The sole purpose of this function is to provide a means of fine tuning QList 's memory usage. In general, you will rarely ever need to call this function.

另請參閱 reserve () 和 capacity ().

bool QList:: startsWith ( QList < T > ::parameter_type value ) const

返迴 true if this list is not empty and its first item is equal to value ;否則返迴 false .

另請參閱 isEmpty () 和 first ().

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

Swaps this list with other 。此操作很快且從不失敗。

void QList:: swapItemsAt ( qsizetype i , qsizetype j )

Exchange the item at index position i with the item at index position j . This function assumes that both i and j are at least 0 but less than size (). To avoid failure, test that both i and j are at least 0 and less than size ().

T QList:: takeAt ( qsizetype i )

Removes the element at index position i 並返迴它。

相當於

T t = at(i);
remove(i);
return t;
					

注意: QList 不是 隱式共享 , this function only invalidates iterators at or after the specified position.

另請參閱 takeFirst () 和 takeLast ().

QList < T > ::value_type QList:: takeFirst ()

Removes the first item in the list and returns it. This function assumes the list is not empty. To avoid failure, call isEmpty () before calling this function.

另請參閱 takeLast () 和 removeFirst ().

QList < T > ::value_type QList:: takeLast ()

Removes the last item in the list and returns it. This function assumes the list is not empty. To avoid failure, call isEmpty () before calling this function.

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

另請參閱 takeFirst () 和 removeLast ().

T QList:: value ( qsizetype i ) const

Returns the value at index position i 在列錶中。

If the index i is out of bounds, the function returns a 默認構造值 . If you are certain that i is within bounds, you can use at () instead, which is slightly faster.

另請參閱 at () 和 operator[] ().

T QList:: value ( qsizetype i , QList < T > ::parameter_type defaultValue ) const

If the index i is out of bounds, the function returns defaultValue .

這是重載函數。

bool QList:: operator!= (const QList < 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 have an implementation of operator==() .

另請參閱 operator== ().

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

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

另請參閱 operator+ () 和 append ().

[since 6.0] QList < T > &QList:: operator+= ( QList < T > && other )

這是重載函數。

該函數在 Qt 6.0 引入。

另請參閱 operator+ () 和 append ().

QList < T > &QList:: operator+= ( QList < T > ::parameter_type value )

追加 value to the list.

這是重載函數。

另請參閱 append () 和 operator<< ().

QList < T > &QList:: operator+= ( QList < T > ::rvalue_ref value )

這是重載函數。

另請參閱 append () 和 operator<< ().

bool QList:: operator< (const QList < T > & other ) const

返迴 true if this list is lexically less than other ;否則返迴 false .

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

QList < T > &QList:: operator<< ( QList < T > ::parameter_type value )

追加 value to the list and returns a reference to this list.

另請參閱 append () 和 operator+= ().

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

追加 other to the list and returns a reference to the list.

[since 6.0] QList < T > &QList:: operator<< ( QList < T > && other )

這是重載函數。

該函數在 Qt 6.0 引入。

QList < T > &QList:: operator<< ( QList < T > ::rvalue_ref value )

這是重載函數。

另請參閱 append () 和 operator+= ().

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

返迴 true if this list is lexically less than or equal to other ;否則返迴 false .

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

[default] QList < T > &QList:: operator= ( QList < T > && other )

移動賦值 other 到此 QList 實例。

[default] QList < T > &QList:: operator= (const QList < T > & other )

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

QList < T > &QList:: operator= ( std::initializer_list < T > args )

Assigns the collection of values in args 到此 QList 實例。

bool QList:: operator== (const QList < 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 have an implementation of operator==() .

另請參閱 operator!= ().

bool QList:: operator> (const QList < T > & other ) const

返迴 true if this list is lexically greater than other ;否則返迴 false .

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

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

返迴 true if this list is lexically greater than or equal to other ;否則返迴 false .

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

QList < T > ::reference QList:: operator[] ( qsizetype i )

返迴項按索引位置 i 作為可修改引用。

i must be a valid index position in the list (i.e., 0 <= i < size ()).

Note that using non-const operators can cause QList to do a deep copy.

另請參閱 at () 和 value ().

[noexcept] QList < T > ::const_reference QList:: operator[] ( qsizetype i ) const

如同 at( i ).

這是重載函數。

相關非成員

[since 6.1] template <typename T, typename AT> qsizetype erase ( QList < T > & list , const AT & t )

Removes all elements that compare equal to t from the list list . Returns the number of elements removed, if any.

注意: 不像 QList::removeAll , t is not allowed to be a reference to an element inside list . If you cannot be sure that this is not the case, take a copy of t and call this function with the copy.

該函數在 Qt 6.1 引入。

另請參閱 QList::removeAll () 和 erase_if .

[since 6.1] template <typename T, typename Predicate> qsizetype erase_if ( QList < T > & list , Predicate pred )

Removes all elements for which the predicate pred returns true from the list list . Returns the number of elements removed, if any.

該函數在 Qt 6.1 引入。

另請參閱 erase .

[noexcept(...)] template <typename T> size_t qHash (const QList < T > & key , size_t seed = 0)

返迴哈希值為 key ,使用 seed 做計算種子。

類型 T must be supported by qHash().

注意: This function is noexcept when noexcept(qHashRange(key.cbegin(), key.cend(), seed)) is true .

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

Writes the list list 到流 out .

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

另請參閱 QDataStream 運算符格式 .

[since 6.9] auto operator<=> (const QList < T > & lhs , const QList < T > & rhs )

Compares the contents of lhs and rhs lexicographically . Returns the result of the strongest applicable category type, that is decltype(lhs[0] <=> rhs[0]) if operator<=>() is available for type T ;否則 std::weak_ordering .

注意: This operator is only available in C++20 mode, and when the underlying type T models the std::three_way_comparable concept or provides operator<() .

該函數在 Qt 6.9 引入。

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

Reads a list from stream in into list .

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

另請參閱 QDataStream 運算符格式 .