QList 类

template <typename T> class QList

QList 类是提供列表的模板类。 更多...

头: #include <QList>
qmake: QT += core
继承者:

QByteArrayList , QItemSelection , QQueue ,和 QStringList

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

公共类型

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

公共函数

QList (InputIterator first , InputIterator last )
QList (std::initializer_list<T> args )
QList (QList<T> && other )
QList (const QList<T> & other )
QList ()
QList<T> & operator= (QList<T> && other )
QList<T> & operator= (const QList<T> & other )
~QList ()
void append (const T & value )
void append (const QList<T> & value )
const T & at (int i ) const
T & back ()
const T & back () const
QList::iterator begin ()
QList::const_iterator begin () const
QList::const_iterator cbegin () const
QList::const_iterator cend () const
void clear ()
QList::const_iterator constBegin () const
QList::const_iterator constEnd () const
const T & constFirst () const
const T & constLast () const
bool contains (const T & value ) const
int count (const T & value ) const
int count () const
QList::const_reverse_iterator crbegin () const
QList::const_reverse_iterator crend () const
bool empty () const
QList::iterator end ()
QList::const_iterator end () const
bool endsWith (const T & value ) const
QList::iterator erase (QList::iterator pos )
QList::iterator erase (QList::iterator begin , QList::iterator end )
T & first ()
const T & first () const
T & front ()
const T & front () const
int indexOf (const T & value , int from = 0) const
void insert (int i , const T & value )
QList::iterator insert (QList::iterator before , const T & value )
bool isEmpty () const
T & last ()
const T & last () const
int lastIndexOf (const T & value , int from = -1) const
int length () const
QList<T> mid (int pos , int length = -1) const
void move (int from , int to )
void pop_back ()
void pop_front ()
void prepend (const T & value )
void push_back (const T & value )
void push_front (const T & value )
QList::reverse_iterator rbegin ()
QList::const_reverse_iterator rbegin () const
int removeAll (const T & value )
void removeAt (int i )
void removeFirst ()
void removeLast ()
bool removeOne (const T & value )
QList::reverse_iterator rend ()
QList::const_reverse_iterator rend () const
void replace (int i , const T & value )
void reserve (int alloc )
int size () const
bool startsWith (const T & value ) const
void swap (QList<T> & other )
void swapItemsAt (int i , int j )
T takeAt (int i )
T takeFirst ()
T takeLast ()
QVector<T> toVector () const
T value (int i ) const
T value (int i , const T & defaultValue ) const
bool operator!= (const QList<T> & other ) const
QList<T> operator+ (const QList<T> & other ) const
QList<T> & operator+= (const QList<T> & other )
QList<T> & operator+= (const T & value )
QList<T> & operator<< (const QList<T> & other )
QList<T> & operator<< (const T & value )
bool operator== (const QList<T> & other ) const
T & operator[] (int i )
const T & operator[] (int i ) const

静态公共成员

QList<T> fromVector (const QVector<T> & vector )
uint qHash (const QList<T> & key , uint seed = 0)
bool operator< (const QList<T> & lhs , const QList<T> & rhs )
QDataStream & operator<< (QDataStream & out , const QList<T> & list )
bool operator<= (const QList<T> & lhs , const QList<T> & rhs )
bool operator> (const QList<T> & lhs , const QList<T> & rhs )
bool operator>= (const QList<T> & lhs , const QList<T> & rhs )
QDataStream & operator>> (QDataStream & in , QList<T> & list )

详细描述

QList 是一种 Qt 一般 容器类 . It stores items in a list that provides fast index-based access and index-based insertions and removals.

QList<T>, QLinkedList<T>, and QVector <T> 提供类似 API 和功能。它们经常可互换,但有性能后果。这里是用例概述:

  • QVector should be your default first choice. QVector <T> will usually give better performance than QList<T>, because QVector <T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE Q_PRIMITIVE_TYPE 使用 Q_DECLARE_TYPEINFO 。见 Pros and Cons of Using QList for an explanation.
  • However, QList is used throughout the Qt APIs for passing parameters and for returning values. Use QList to interface with those APIs.
  • If you need a real linked list, which guarantees 常量时间 insertions mid-list and uses iterators to items rather than indexes, use QLinkedList.

注意: QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.

注意: Iterators into a QLinkedList and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a QVector and non-heap-allocating QLists.

Internally, QList<T> is represented as an array of T if sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE Q_PRIMITIVE_TYPE 使用 Q_DECLARE_TYPEINFO . Otherwise, QList<T> is represented as an array of T* and the items are allocated on the heap.

The array representation allows very fast insertions and index-based access. The prepend () 和 append () operations are also very fast because QList preallocates memory at both ends of its internal array. (See 算法的复杂性 了解细节。

Note, however, that when the conditions specified above are not met, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation will make QVector a better choice for use cases that do a lot of appending or inserting, because QVector can allocate memory for many items in a single heap allocation.

Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.

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

QList<int> integerList;
QList<QDate> dateList;
					

Qt includes a QStringList class that inherits QList< QString > and adds a few convenience functions, such as QStringList::join () 和 QStringList::filter (). QString::split () creates QStringLists from strings.

QList stores a list of items. The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:

QList<QString> list = { "one", "two", "three" };
					

QList provides these basic functions to add, move, and remove items: insert (), replace (), removeAt (), move (),和 swap (). In addition, it provides the following convenience functions: append (), operator<< (), operator+= (), prepend (), removeFirst (),和 removeLast ().

operator<< () allows to conveniently add multiple elements to a list:

list << "four" << "five";
					

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 and can be used on the left side of an assignment:

if (list[0] == "Bob")
    list[0] = "Robert";
					

Because QList is implemented as an array of pointers for types that are larger than a pointer or are not movable, this operation requires ( 常量时间 ). For read-only access, an alternative syntax is to use at ():

for (int i = 0; i < list.size(); ++i) {
    if (list.at(i) == "Jane")
        cout << "Found Jane at position " << i << Qt::endl;
}
					

at () can be faster than operator[](), because it never causes a 深拷贝 的出现。

A common requirement is to remove an item from a list and do something with it. For this, QList provides takeAt (), takeFirst (),和 takeLast (). Here's a loop that removes the items from a list one at a time and calls delete on them:

QList<QWidget *> list;
...
while (!list.isEmpty())
    delete list.takeFirst();
					

Inserting and removing items at either end of the list is very fast ( 常量时间 in most cases), because QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.

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 a matching item if they find it; otherwise, they return -1. For example:

int i = list.indexOf("Jane");
if (i != -1)
    cout << "First occurrence of Jane is at position " << i << Qt::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 (). If you want to replace all occurrences of a particular value with another, use replace ().

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.

Like the other container classes, QList provides Java 风格迭代器 ( QListIterator and QMutableListIterator ) 和 STL 样式迭代器 ( QList::const_iterator and QList::iterator ). In practice, these are rarely used, because you can use indexes into the QList. QList is implemented in such a way that direct index-based access is just as fast as using iterators.

QList does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.

To make QList as efficient as possible, its member functions don't validate their input before using it. Except for isEmpty (), member functions always assume the list is not empty. Member functions that take index values as parameters always assume their index value parameters are in the valid range. This means QList member functions can fail. If you define QT_NO_DEBUG when you compile, failures will not be detected. If you don't define QT_NO_DEBUG, failures will be detected using Q_ASSERT () 或 Q_ASSERT_X () with an appropriate message.

To avoid failures when your list can be empty, call isEmpty () before calling other member functions. If you must pass an index value that might not be in the valid range, check that it is less than the value returned by size () but not less than 0.

更多成员

若 T 是 QByteArray , this class has a couple more members that can be used. See the documentation for QByteArrayList 了解更多信息。

若 T 是 QString , this class has the following additional members: filter , join , removeDuplicates , sort .

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

有关比较 Qt 各种容器和 STL 容器的详细讨论,见 理解 Qt 容器 .

另请参阅 QListIterator , QMutableListIterator , QLinkedList ,和 QVector .

成员类型文档编制

typedef QList:: ConstIterator

Qt 样式同义词 QList::const_iterator .

typedef QList:: Iterator

Qt 样式同义词 QList::iterator .

typedef QList:: const_pointer

Typedef for const T *. Provided for STL compatibility.

typedef QList:: const_reference

Typedef for const T &. Provided for STL compatibility.

typedef QList:: const_reverse_iterator

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

It is simply a typedef for std::reverse_iterator<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 隐式共享迭代器问题 .

该 typedef 在 Qt 5.6 引入。

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

typedef QList:: difference_type

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

typedef QList:: pointer

Typedef for T *. Provided for STL compatibility.

typedef QList:: reference

Typedef for T &. Provided for STL compatibility.

typedef QList:: reverse_iterator

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

It is simply a typedef for std::reverse_iterator<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 隐式共享迭代器问题 .

该 typedef 在 Qt 5.6 引入。

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

typedef QList:: size_type

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

typedef QList:: value_type

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

成员函数文档编制

template <typename InputIterator> QList:: QList ( InputIterator first , InputIterator last )

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

The value type of InputIterator must be convertible to T .

该函数在 Qt 5.14 引入。

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

Construct a list from the std::initializer_list specified by args .

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

该函数在 Qt 4.8 引入。

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

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

该函数在 Qt 5.2 引入。

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

QList:: QList ()

构造空列表。

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

移动赋值 other 到此 QList 实例。

该函数在 Qt 5.2 引入。

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

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

QList:: ~QList ()

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

void QList:: append (const T & value )

插入 value 在列表末尾。

范例:

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

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

If this list is not shared, this operation is typically very fast (amortized 常量时间 ),因为 QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.

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

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

这是重载函数。

Appends the items of the value list to this list.

该函数在 Qt 4.5 引入。

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

const T &QList:: at ( int i ) const

返回项按索引位置 i 在列表中。 i must be a valid index position in the list (i.e., 0 <= i < size ()).

This function is very fast ( 常量时间 ).

另请参阅 value () 和 operator[] ().

T &QList:: back ()

此函数为兼容 STL (标准模板库) 提供。它相当于 last (). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

const T &QList:: back () const

这是重载函数。

QList::iterator QList:: begin ()

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

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

QList::const_iterator QList:: begin () const

这是重载函数。

QList::const_iterator QList:: cbegin () const

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

该函数在 Qt 5.0 引入。

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

QList::const_iterator QList:: cend () const

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

该函数在 Qt 5.0 引入。

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

void QList:: clear ()

Removes all items from the list.

另请参阅 removeAll ().

QList::const_iterator QList:: constBegin () const

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

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

QList::const_iterator QList:: constEnd () const

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

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

const T &QList:: constFirst () const

Returns a const reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

该函数在 Qt 5.6 引入。

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

const T &QList:: constLast () const

Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

该函数在 Qt 5.6 引入。

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

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

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

int QList:: 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 () 和 indexOf ().

int QList:: count () const

Returns the number of items in the list. This is effectively the same as size ().

QList::const_reverse_iterator QList:: crbegin () const

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

该函数在 Qt 5.6 引入。

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

QList::const_reverse_iterator QList:: crend () const

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

该函数在 Qt 5.6 引入。

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

bool QList:: empty () const

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

QList::iterator QList:: end ()

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

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

QList::const_iterator QList:: end () const

这是重载函数。

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

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

该函数在 Qt 4.5 引入。

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

QList::iterator QList:: erase ( QList::iterator pos )

Removes the item associated with the iterator pos from the list, and returns an iterator to the next item in the list (which may be end ()).

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

QList::iterator QList:: erase ( QList::iterator begin , QList::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.

T &QList:: first ()

Returns a reference to the first item in the list. The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

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

const T &QList:: first () const

这是重载函数。

[static] QList < T > QList:: fromVector (const QVector < T > & vector )

返回 QList object with the data contained in vector .

范例:

QVector<double> vect;
vect << 20.0 << 30.0 << 40.0 << 50.0;
QList<double> list = QList<double>::fromVector(vect);
// list: [20.0, 30.0, 40.0, 50.0]
					

注意: Since Qt 5.14, range constructors are available for Qt's generic 容器类 and should be used in place of this method.

另请参阅 toVector () 和 QVector::toList ().

T &QList:: front ()

此函数为兼容 STL (标准模板库) 提供。它相当于 first (). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

const T &QList:: front () const

这是重载函数。

int QList:: indexOf (const T & value , int 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;
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==() .

注意, QList uses 0-based indexes, just like C++ arrays. Negative indexes are not supported with the exception of the value mentioned above.

另请参阅 lastIndexOf () 和 contains ().

void QList:: insert ( int i , const T & value )

插入 value at index position i 在列表中。

i == 0, the value is prepended to the list. If i == size (), the value is appended to the list.

范例:

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

另请参阅 append (), prepend (), replace (),和 removeAt ().

QList::iterator QList:: insert ( QList::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. Note that the iterator passed to the function will be invalid after the call; the returned iterator should be used instead.

bool QList:: isEmpty () const

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

另请参阅 size ().

T &QList:: last ()

Returns a reference to the last item in the list. The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

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

const T &QList:: last () const

这是重载函数。

int QList:: lastIndexOf (const T & value , int from = -1) const

Returns the index position of the last occurrence of 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;
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==() .

注意, QList uses 0-based indexes, just like C++ arrays. Negative indexes are not supported with the exception of the value mentioned above.

另请参阅 indexOf ().

int QList:: length () const

This function is identical to count ().

该函数在 Qt 4.5 引入。

另请参阅 count ().

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

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

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

移动项按索引位置 from 到索引位置 to .

范例:

QList<QString> list;
list << "A" << "B" << "C" << "D" << "E" << "F";
list.move(1, 4);
// list: ["A", "C", "D", "E", "B", "F"]
					

这如同 insert( to , takeAt ( from )).This function assumes that both from and to are at least 0 but less than size (). To avoid failure, test that both from and to are at least 0 and less than size ().

另请参阅 swap (), insert (),和 takeAt ().

void QList:: pop_back ()

此函数为兼容 STL (标准模板库) 提供。它相当于 removeLast (). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

void QList:: pop_front ()

此函数为兼容 STL (标准模板库) 提供。它相当于 removeFirst (). The list must not be empty. If the list can be empty, call isEmpty () before calling this function.

void QList:: prepend (const T & 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 ).

If this list is not shared, this operation is typically very fast (amortized 常量时间 ),因为 QList preallocates extra space on both sides of its internal buffer to allow for fast growth at both ends of the list.

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

void QList:: push_back (const T & value )

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

void QList:: push_front (const T & value )

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

QList::reverse_iterator QList:: rbegin ()

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

该函数在 Qt 5.6 引入。

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

QList::const_reverse_iterator QList:: rbegin () const

这是重载函数。

该函数在 Qt 5.6 引入。

int QList:: removeAll (const T & value )

Removes all occurrences of value in the list and returns the number of entries removed.

范例:

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

另请参阅 removeOne (), removeAt (), takeAt (),和 replace ().

void QList:: removeAt ( int i )

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

另请参阅 takeAt (), removeFirst (), removeLast (),和 removeOne ().

void QList:: removeFirst ()

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

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

void QList:: removeLast ()

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

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

bool QList:: removeOne (const T & value )

Removes the first occurrence of value in the list and returns true on success; otherwise returns 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==() .

该函数在 Qt 4.4 引入。

另请参阅 removeAll (), removeAt (), takeAt (),和 replace ().

QList::reverse_iterator QList:: rend ()

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

该函数在 Qt 5.6 引入。

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

QList::const_reverse_iterator QList:: rend () const

这是重载函数。

该函数在 Qt 5.6 引入。

void QList:: replace ( int i , const T & value )

替换项在索引位置 i with value . i must be a valid index position in the list (i.e., 0 <= i < size ()).

另请参阅 operator[] () 和 removeAt ().

void QList:: reserve ( int alloc )

Reserve space for alloc 元素。

alloc is smaller than the current size of the list, nothing will happen.

Use this function to avoid repetetive reallocation of QList 's internal data if you can predict how many elements will be appended. Note that the reservation applies only to the internal pointer array.

该函数在 Qt 4.7 引入。

int QList:: size () const

Returns the number of items in the list.

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

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

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

该函数在 Qt 4.5 引入。

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

void QList:: swap ( QList < T > & other )

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

该函数在 Qt 4.8 引入。

void QList:: swapItemsAt ( int i , int 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 ().

范例:

QList<QString> list;
list << "A" << "B" << "C" << "D" << "E" << "F";
list.swapItemsAt(1, 4);
// list: ["A", "E", "C", "D", "B", "F"]
					

该函数在 Qt 5.13 引入。

另请参阅 move ().

T QList:: takeAt ( int i )

Removes the item at index position i and returns it. i must be a valid index position in the list (i.e., 0 <= i < size ()).

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

另请参阅 removeAt (), takeFirst (),和 takeLast ().

T QList:: takeFirst ()

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

If this list is not shared, this operation takes 常量时间 .

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

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

T QList:: takeLast ()

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

If this list is not shared, this operation takes 常量时间 .

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

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

QVector < T > QList:: toVector () const

返回 QVector object with the data contained in this QList .

范例:

QStringList list;
list << "Sven" << "Kim" << "Ola";
QVector<QString> vect = list.toVector();
// vect: ["Sven", "Kim", "Ola"]
					

注意: Since Qt 5.14, range constructors are available for Qt's generic 容器类 and should be used in place of this method.

另请参阅 fromVector () 和 QVector::fromList ().

T QList:: value ( int 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 the index is going to be within bounds, you can use at () instead, which is slightly faster.

另请参阅 at () 和 operator[] ().

T QList:: value ( int i , const T & 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 ) const

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

另请参阅 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 ().

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

这是重载函数。

追加 value to the list.

另请参阅 append () 和 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 ().

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

这是重载函数。

追加 value to the list.

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

T &QList:: operator[] ( int i )

返回项按索引位置 i 作为可修改引用。 i must be a valid index position in the list (i.e., 0 <= i < size ()).

If this function is called on a list that is currently being shared, it will trigger a copy of all elements. Otherwise, this function runs in 常量时间 . If you do not want to modify the list you should use QList::at ().

另请参阅 at () 和 value ().

const T &QList:: operator[] ( int i ) const

这是重载函数。

如同 at (). This function runs in 常量时间 .

相关非成员

template <typename T> uint qHash (const QList < T > & key , uint seed = 0)

返回哈希值为 key ,使用 seed 做计算种子。

This function requires qHash() to be overloaded for the value type T .

该函数在 Qt 5.6 引入。

template <typename T> bool operator< (const QList < T > & lhs , const QList < T > & rhs )

返回 true if list lhs is lexicographically less than rhs ;否则返回 false .

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

该函数在 Qt 5.6 引入。

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 运算符格式 .

template <typename T> bool operator<= (const QList < T > & lhs , const QList < T > & rhs )

返回 true if list lhs is lexicographically less than or equal to rhs ;否则返回 false .

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

该函数在 Qt 5.6 引入。

template <typename T> bool operator> (const QList < T > & lhs , const QList < T > & rhs )

返回 true if list lhs is lexicographically greater than rhs ;否则返回 false .

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

该函数在 Qt 5.6 引入。

template <typename T> bool operator>= (const QList < T > & lhs , const QList < T > & rhs )

返回 true if list lhs is lexicographically greater than or equal to rhs ;否则返回 false .

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

该函数在 Qt 5.6 引入。

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 运算符格式 .