QVector 類

The QVector class is a template class that provides a dynamic array. 更多...

頭: #include <QVector>
qmake: QT += core
繼承者: QPolygon , QPolygonF , QStack ,和 QXmlStreamAttributes

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

公共類型

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

公共函數

QVector ()
QVector (int size )
QVector (int size , const T & value )
QVector (const QVector<T> & other )
QVector (QVector<T> && other )
QVector (std::initializer_list<T> args )
~QVector ()
void append (const T & value )
void append (T && value )
void append (const QVector<T> & value )
const T & at (int i ) const
reference back ()
const_reference back () const
iterator begin ()
const_iterator begin () const
int capacity () const
const_iterator cbegin () const
const_iterator cend () const
void clear ()
const_iterator constBegin () const
const T * constData () const
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
const_reverse_iterator crbegin () const
const_reverse_iterator crend () const
T * data ()
const T * data () const
bool empty () const
iterator end ()
const_iterator end () const
bool endsWith (const T & value ) const
iterator erase (iterator pos )
iterator erase (iterator begin , iterator end )
QVector<T> & fill (const T & value , int size = -1)
T & first ()
const T & first () const
T & front ()
const_reference front () const
int indexOf (const T & value , int from = 0) const
void insert (int i , const T & value )
void insert (int i , int count , const T & value )
iterator insert (iterator before , int count , const T & value )
iterator insert (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
QVector<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_back (T && value )
void push_front (const T & value )
reverse_iterator rbegin ()
const_reverse_iterator rbegin () const
void remove (int i )
void remove (int i , int count )
int removeAll (const T & t )
void removeAt (int i )
void removeFirst ()
void removeLast ()
bool removeOne (const T & t )
reverse_iterator rend ()
const_reverse_iterator rend () const
void replace (int i , const T & value )
void reserve (int size )
void resize (int size )
int size () const
void squeeze ()
bool startsWith (const T & value ) const
void swap (QVector<T> & other )
T takeAt (int i )
T takeFirst ()
T takeLast ()
QList<T> toList () const
std::vector<T> toStdVector () const
T value (int i ) const
T value (int i , const T & defaultValue ) const
bool operator!= (const QVector<T> & other ) const
QVector<T> operator+ (const QVector<T> & other ) const
QVector<T> & operator+= (const QVector<T> & other )
QVector<T> & operator+= (const T & value )
QVector<T> & operator<< (const T & value )
QVector<T> & operator<< (const QVector<T> & other )
QVector<T> & operator= (const QVector<T> & other )
QVector<T> & operator= (QVector<T> && other )
bool operator== (const QVector<T> & other ) const
T & operator[] (int i )
const T & operator[] (int i ) const

靜態公共成員

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

詳細描述

The QVector class is a template class that provides a dynamic array.

QVector <T> is one of Qt's generic 容器類 。它將項存儲在相鄰內存位置,並提供基於索引的快速訪問。

QList <T>, QLinkedList <T>, QVector <T>, and QVarLengthArray <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.
  • 不管怎樣, 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 兩者均保證兼容 C 數組布局。 QList 不。這可能很重要,若應用程序必須接口 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.

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

QVector<int> integerVector;
QVector<QString> stringVector;
					

QVector stores its items in a vector (array). Typically, vectors are created with an initial size. For example, the following code constructs a QVector with 200 elements:

QVector<QString> vector(200);
					

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

QVector<QString> vector(200, "Pass");
					

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

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

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

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

for (int i = 0; i < vector.size(); ++i) {
    if (vector.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 QVector 是調用 data (). The function returns a pointer to the first item in the vector. You can use the pointer to directly access and modify the elements stored in the vector. The pointer is also useful if you need to pass a QVector to a function that accepts a plain C++ array.

If you want to find all occurrences of a particular value in a vector, 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:

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

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

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

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

If you know in advance approximately how many items the QVector will contain, you can call reserve (), asking QVector to preallocate a certain amount of memory. You can also call capacity () to find out how much memory QVector actually allocated.

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

QVector '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, QVector 提供 Java 風格迭代器 ( QVectorIterator and QMutableVectorIterator ) 和 STL 樣式迭代器 ( QVector::const_iterator and QVector::iterator ). In practice, these are rarely used, because you can use indexes into the QVector .

除瞭 QVector , Qt also provides QVarLengthArray ,具有小的功能速度優化的很低級類。

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

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

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

另請參閱 QVectorIterator , QMutableVectorIterator , QList ,和 QLinkedList .

成員類型文檔編製

typedef QVector:: ConstIterator

Qt 樣式同義詞 QVector::const_iterator .

typedef QVector:: Iterator

Qt 樣式同義詞 QVector::iterator .

typedef QVector:: const_iterator

The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack .

QVector provides both STL 樣式迭代器 and Java 風格迭代器 . The STL-style const iterator is simply a typedef for "const T *" (pointer to const T).

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

另請參閱 QVector::constBegin (), QVector::constEnd (), QVector::iterator ,和 QVectorIterator .

typedef QVector:: const_pointer

Typedef for const T *. Provided for STL compatibility.

typedef QVector:: const_reference

Typedef for T &. Provided for STL compatibility.

typedef QVector:: const_reverse_iterator

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

It is simply a typedef for std::reverse_iterator<const T*> .

警告: 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 引入。

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

typedef QVector:: difference_type

typedef 對於 ptrdiff_t。為兼容 STL 提供。

typedef QVector:: iterator

The QVector::iterator typedef provides an STL-style non-const iterator for QVector and QStack .

QVector provides both STL 樣式迭代器 and Java 風格迭代器 . The STL-style non-const iterator is simply a typedef for "T *" (pointer to T).

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

另請參閱 QVector::begin (), QVector::end (), QVector::const_iterator ,和 QMutableVectorIterator .

typedef QVector:: pointer

Typedef for T *. Provided for STL compatibility.

typedef QVector:: reference

Typedef for T &. Provided for STL compatibility.

typedef QVector:: reverse_iterator

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

It is simply a typedef for std::reverse_iterator<T*> .

警告: 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 引入。

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

typedef QVector:: size_type

typedef 對於 int。為兼容 STL 提供。

typedef QVector:: value_type

typedef 對於 T。為兼容 STL 提供。

成員函數文檔編製

QVector:: QVector ()

Constructs an empty vector.

另請參閱 resize ().

QVector:: QVector ( int size )

Constructs a vector with an initial size of size 元素。

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

另請參閱 resize ().

QVector:: QVector ( int size , const T & value )

Constructs a vector with an initial size of size elements. Each element is initialized with value .

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

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

構造副本為 other .

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

另請參閱 operator= ().

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

移動構造 QVector 實例,使之指嚮同一對象如 other 所指嚮的。

該函數在 Qt 5.2 引入。

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

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

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

該函數在 Qt 4.8 引入。

QVector:: ~QVector ()

Destroys the vector.

void QVector:: append (const T & value )

插入 value at the end of the vector.

範例:

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

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

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

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

void QVector:: append ( T && value )

這是重載函數。

範例:

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

該函數在 Qt 5.6 引入。

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

這是重載函數。

Appends the items of the value vector to this vector.

該函數在 Qt 5.5 引入。

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

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

返迴項按索引位置 i in the vector.

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

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

reference QVector:: back ()

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

const_reference QVector:: back () const

這是重載函數。

iterator QVector:: begin ()

返迴 STL 樣式迭代器 pointing to the first item in the vector.

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

const_iterator QVector:: begin () const

這是重載函數。

int QVector:: capacity () const

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

The sole purpose of this function is to provide a means of fine tuning QVector '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 vector, call size ().

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

const_iterator QVector:: cbegin () const

返迴常量 STL 樣式迭代器 pointing to the first item in the vector.

該函數在 Qt 5.0 引入。

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

const_iterator QVector:: cend () const

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the vector.

該函數在 Qt 5.0 引入。

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

void QVector:: clear ()

Removes all the elements from the vector.

注意: Until Qt 5.6, this also released the memory used by the vector. From Qt 5.7, the capacity is preserved. To shed all capacity, swap with a default-constructed vector:

QVector<T> v ...;
QVector<T>().swap(v);
Q_ASSERT(v.capacity() == 0);
					

or call squeeze ().

另請參閱 squeeze ().

const_iterator QVector:: constBegin () const

返迴常量 STL 樣式迭代器 pointing to the first item in the vector.

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

const T *QVector:: constData () const

Returns a const pointer to the data stored in the vector. The pointer can be used to access the items in the vector. The pointer remains valid as long as the vector isn't reallocated.

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

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

const_iterator QVector:: constEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the vector.

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

const T &QVector:: constFirst () const

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

該函數在 Qt 5.6 引入。

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

const T &QVector:: constLast () const

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

該函數在 Qt 5.6 引入。

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

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

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

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

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

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

Returns the number of occurrences of value in the vector.

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

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

int QVector:: count () const

這是重載函數。

如同 size ().

const_reverse_iterator QVector:: crbegin () const

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

該函數在 Qt 5.6 引入。

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

const_reverse_iterator QVector:: crend () const

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

該函數在 Qt 5.6 引入。

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

T *QVector:: data ()

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

範例:

QVector<int> vector(10);
int *data = vector.data();
for (int i = 0; i < 10; ++i)
    data[i] = 2 * i;
					

The pointer remains valid as long as the vector isn't reallocated.

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

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

const T *QVector:: data () const

這是重載函數。

bool QVector:: empty () const

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

iterator QVector:: end ()

返迴 STL 樣式迭代器 pointing to the imaginary item after the last item in the vector.

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

const_iterator QVector:: end () const

這是重載函數。

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

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

該函數在 Qt 4.5 引入。

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

iterator QVector:: erase ( iterator pos )

Removes the item pointed to by the iterator pos from the vector, and returns an iterator to the next item in the vector (which may be end ()).

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

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

QVector < T > &QVector:: fill (const T & value , int size = -1)

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

範例:

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

另請參閱 resize ().

T &QVector:: first ()

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

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

const T &QVector:: first () const

這是重載函數。

[static] QVector < T > QVector:: fromList (const QList < T > & list )

返迴 QVector object with the data contained in list .

範例:

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

另請參閱 toList () 和 QList::toVector ().

[static] QVector < T > QVector:: fromStdVector (const std::vector < T > & vector )

返迴 QVector object with the data contained in vector . The order of the elements in the QVector is the same as in vector .

範例:

std::vector<double> stdvector;
vector.push_back(1.2);
vector.push_back(0.5);
vector.push_back(3.14);
QVector<double> vector = QVector<double>::fromStdVector(stdvector);
					

另請參閱 toStdVector () 和 QList::fromStdList ().

T &QVector:: front ()

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

const_reference QVector:: front () const

這是重載函數。

int QVector:: indexOf (const T & value , int from = 0) const

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

範例:

QVector<QString> vector;
vector << "A" << "B" << "C" << "B" << "A";
vector.indexOf("B");            // returns 1
vector.indexOf("B", 1);         // returns 1
vector.indexOf("B", 2);         // returns 3
vector.indexOf("X");            // returns -1
					

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

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

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

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

範例:

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

For large vectors, 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 QLinkedList 代替。

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

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

這是重載函數。

插入 count 個副本對於 value at index position i in the vector.

範例:

QVector<double> vector;
vector << 2.718 << 1.442 << 0.4342;
vector.insert(1, 3, 9.9);
// vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
					

iterator QVector:: insert ( iterator before , int count , const T & 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.

iterator QVector:: insert ( 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.

bool QVector:: isEmpty () const

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

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

T &QVector:: last ()

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

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

const T &QVector:: last () const

這是重載函數。

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

Returns the index position of the last occurrence of the value value in the vector, 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> vector;
vector << "A" << "B" << "C" << "B" << "A";
vector.lastIndexOf("B");        // returns 3
vector.lastIndexOf("B", 3);     // returns 3
vector.lastIndexOf("B", 2);     // returns 1
vector.lastIndexOf("X");        // returns -1
					

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

另請參閱 indexOf ().

int QVector:: length () const

如同 size () 和 count ().

Provided for compatibility with QList .

該函數在 Qt 5.2 引入。

另請參閱 size (), count (),和 QList::length ().

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

Returns a sub-vector which contains elements from this vector, 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 QVector:: move ( int from , int to )

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

Provided for compatibility with QList .

該函數在 Qt 5.6 引入。

另請參閱 QList::move ().

void QVector:: pop_back ()

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

void QVector:: pop_front ()

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

void QVector:: prepend (const T & value )

插入 value at the beginning of the vector.

範例:

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

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

For large vectors, this operation can be slow ( 綫性時間 ), because it requires moving all the items in the vector by one position further in memory. If you want a container class that provides a fast prepend() function, use QList or QLinkedList 代替。

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

void QVector:: push_back (const T & value )

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

void QVector:: push_back ( T && value )

這是重載函數。

該函數在 Qt 5.6 引入。

void QVector:: push_front (const T & value )

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

reverse_iterator QVector:: rbegin ()

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

該函數在 Qt 5.6 引入。

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

const_reverse_iterator QVector:: rbegin () const

這是重載函數。

該函數在 Qt 5.6 引入。

void QVector:: remove ( int i )

這是重載函數。

Removes the element at index position i .

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

void QVector:: remove ( int i , int count )

這是重載函數。

移除 count elements from the middle of the vector, starting at index position i .

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

int QVector:: removeAll (const T & t )

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

Provided for compatibility with QList .

該函數在 Qt 5.4 引入。

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

void QVector:: removeAt ( int i )

Removes the element at index position i . Equivalent to

remove(i);
					

Provided for compatibility with QList .

該函數在 Qt 5.2 引入。

另請參閱 remove () 和 QList::removeAt ().

void QVector:: removeFirst ()

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

該函數在 Qt 5.1 引入。

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

void QVector:: removeLast ()

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

該函數在 Qt 5.1 引入。

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

bool QVector:: removeOne (const T & t )

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

Provided for compatibility with QList .

該函數在 Qt 5.4 引入。

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

reverse_iterator QVector:: rend ()

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

該函數在 Qt 5.6 引入。

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

const_reverse_iterator QVector:: rend () const

這是重載函數。

該函數在 Qt 5.6 引入。

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

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

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

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

void QVector:: reserve ( int size )

試圖分配內存為至少 size elements. If you know in advance how large the vector will be, you should call this function to prevent reallocations and memory fragmentation.

size is an underestimate, the worst that will happen is that the QVector will be a bit slower. If size is an overestimate, you may have used more memory than the normal QVector growth strategy would have allocated—or you may have used less.

An alternative to reserve() is calling resize (). Whether or not that is faster than reserve() depends on the element type, because resize () default-constructs all elements, and requires assignment to existing entries rather than calling append (), which copy- or move-constructs. For simple types, like int or double , resize () is typically faster, but for anything more complex, you should prefer reserve().

警告: If the size passed to resize () was underestimated, you run out of allocated space and into undefined behavior. This problem does not exist with reserve(), because it treats the size as just a hint.

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

void QVector:: resize ( int size )

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

Since Qt 5.6, resize() doesn't shrink the capacity anymore. To shed excess capacity, use squeeze ().

另請參閱 size ().

int QVector:: size () const

Returns the number of items in the vector.

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

void QVector:: squeeze ()

Releases any memory not required to store the items.

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

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

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

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

該函數在 Qt 4.5 引入。

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

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

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

該函數在 Qt 4.8 引入。

T QVector:: takeAt ( int i )

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

相當於

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

Provided for compatibility with QList .

該函數在 Qt 5.2 引入。

另請參閱 takeFirst (), takeLast (),和 QList::takeAt ().

T QVector:: takeFirst ()

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

該函數在 Qt 5.1 引入。

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

T QVector:: takeLast ()

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

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

該函數在 Qt 5.1 引入。

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

QList < T > QVector:: toList () const

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

範例:

QVector<double> vect;
vect << "red" << "green" << "blue" << "black";
QList<double> list = vect.toList();
// list: ["red", "green", "blue", "black"]
					

另請參閱 fromList () 和 QList::fromVector ().

std::vector < T > QVector:: toStdVector () const

Returns a std::vector object with the data contained in this QVector 。範例:

QVector<double> vector;
vector << 1.2 << 0.5 << 3.14;
std::vector<double> stdvector = vector.toStdVector();
					

另請參閱 fromStdVector () 和 QList::toStdList ().

T QVector:: value ( int i ) const

Returns the value at index position i in the vector.

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

這是重載函數。

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

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

返迴 true if other is not equal to this vector; otherwise returns false .

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

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

Returns a vector that contains all the items in this vector followed by all the items in the other vector.

另請參閱 operator+= ().

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

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

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

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

這是重載函數。

追加 value to the vector.

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

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

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

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

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

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

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

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

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

移動賦值 other 到此 QVector 實例。

該函數在 Qt 5.2 引入。

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

返迴 true if other is equal to this vector; otherwise returns false .

Two vectors 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 &QVector:: operator[] ( int i )

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

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

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

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

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

這是重載函數。

如同 at( i ).

相關非成員

uint qHash (const QVector < T > & key , uint seed = 0)

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

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

該函數在 Qt 5.6 引入。

bool operator< (const QVector < T > & lhs , const QVector < T > & rhs )

返迴 true if vector lhs is lexicographically less than rhs ;否則返迴 false .

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

該函數在 Qt 5.6 引入。

QDataStream & operator<< ( QDataStream & out , const QVector < T > & vector )

Writes the vector vector 到流 out .

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

另請參閱 QDataStream 運算符格式 .

bool operator<= (const QVector < T > & lhs , const QVector < T > & rhs )

返迴 true if vector 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 引入。

bool operator> (const QVector < T > & lhs , const QVector < T > & rhs )

返迴 true if vector lhs is lexicographically greater than rhs ;否則返迴 false .

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

該函數在 Qt 5.6 引入。

bool operator>= (const QVector < T > & lhs , const QVector < T > & rhs )

返迴 true if vector 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 引入。

QDataStream & operator>> ( QDataStream & in , QVector < T > & vector )

Reads a vector from stream in into vector .

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

另請參閱 QDataStream 運算符格式 .