QVarLengthArray Class ReferenceThe QVarLengthArray class provides a low-level variable-length array. Далее... #include <QVarLengthArray> Замечание: Все функции в этом классе реентерабельны. Открытые типы
Открытые функции
Подробное описаниеThe QVarLengthArray class provides a low-level variable-length array. The C++ language doesn't support variable-length arrays on the stack. For example, the following code won't compile: int myfunc(int n) { int table[n + 1]; // WRONG ... return table[n]; } The alternative is to allocate the array on the heap (with new): int myfunc(int n) { int *table = new int[n + 1]; ... int ret = table[n]; delete[] table; return ret; } However, if myfunc() is called very frequently from the application's inner loop, heap allocation can be a major source of slowdown. QVarLengthArray is an attempt to work around this gap in the C++ language. It allocates a certain number of elements on the stack, and if you resize the array to a larger size, it automatically uses the heap instead. Stack allocation has the advantage that it is much faster than heap allocation. Пример: int myfunc(int n) { QVarLengthArray<int, 1024> array(n + 1); ... return array[n]; } In the example above, QVarLengthArray will preallocate 1024 elements on the stack and use them unless n + 1 is greater than 1024. If you omit the second template argument, QVarLengthArray's default of 256 is used. QVarLengthArray's value type must be an assignable data type. Это охватывает большинство обычно используемых типов данных, но компилятор не позволит вам хранить как значение, например, QWidget; вместо этого сохраняйте QWidget *. QVarLengthArray, like QVector, provides a resizable array data structure. The main differences between the two classes are:
In summary, QVarLengthArray is a low-level optimization class that only makes sense in very specific cases. It is used a few places inside Qt and was added to Qt's public API for the convenience of advanced users. See also QVector, QList, and QLinkedList. Описание типов-членовtypedef QVarLengthArray::const_pointerПсевдоним типа для const T *. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::const_referenceTypedef for const T &. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::difference_typeПсевдоним типа для ptrdiff_t. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::pointerПсевдоним типа для T *. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::referenceПсевдоним типа для T &. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::size_typeПсевдоним типа для int. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. typedef QVarLengthArray::value_typeПсевдоним типа для T. Предоставлен для совместимости с STL. This typedef was introduced in Qt 4.7. Описание функций-членовQVarLengthArray::QVarLengthArray ( int size = 0 )Constructs an array with an initial size of size elements. If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), the elements are not initialized. For other types, the elements are initialized with a default-constructed value. QVarLengthArray::QVarLengthArray ( const QVarLengthArray<T, Prealloc> & other )Создаёт копию other. QVarLengthArray::~QVarLengthArray ()Destroys the array. void QVarLengthArray::append ( const T & t )Appends item t to the array, extending the array if necessary. See also removeLast(). void QVarLengthArray::append ( const T * buf, int size )Appends size amount of items referenced by buf to this array. const T & QVarLengthArray::at ( int i ) constReturns a reference to the item at index position i. i must be a valid index position in the array (i.e., 0 <= i < size()). Смотрите также value() и operator[](). int QVarLengthArray::capacity () constReturns the maximum number of elements that can be stored in the array without forcing a reallocation. The sole purpose of this function is to provide a means of fine tuning QVarLengthArray's memory usage. Вообще, у вас крайне редко возникнет необходимость вызывать эту функцию. If you want to know how many items are in the array, call size(). See also reserve(). void QVarLengthArray::clear ()Removes all the elements from the array. То же самое, что и resize(0). const T * QVarLengthArray::constData () constReturns a const pointer to the data stored in the array. The pointer can be used to access the items in the array. Указатель остается действительным до тех пор, пока массив не будет перераспределен в памяти. This function is mostly useful to pass an array to a function that accepts a plain C++ array. Смотрите также data() и operator[](). int QVarLengthArray::count () constТо же, что и size(). Смотрите также isEmpty() и resize(). T * QVarLengthArray::data ()Returns a pointer to the data stored in the array. The pointer can be used to access and modify the items in the array. Пример: QVarLengthArray<int> array(10); int *data = array.data(); for (int i = 0; i < 10; ++i) data[i] = 2 * i; Указатель остается действительным до тех пор, пока массив не будет перераспределен в памяти. This function is mostly useful to pass an array to a function that accepts a plain C++ array. Смотрите также constData() и operator[](). const T * QVarLengthArray::data () constЭто перегруженная функция. bool QVarLengthArray::isEmpty () constReturns true if the array has size 0; otherwise returns false. Смотрите также size() и resize(). void QVarLengthArray::removeLast ()Decreases the size of the array by one. The allocated size is not changed. Эта функция была введена в Qt 4.5. Смотрите также append(). void QVarLengthArray::reserve ( int size )Пытается зарезервировать место в памяти размером, по крайней мере, size элементов. If you know in advance how large the array can get, you can call this function and if you call resize() often, you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QVarLengthArray will be a bit slower. The sole purpose of this function is to provide a means of fine tuning QVarLengthArray's memory usage. Вообще, у вас крайне редко возникнет необходимость вызывать эту функцию. If you want to change the size of the array, call resize(). See also capacity(). void QVarLengthArray::resize ( int size )Sets the size of the array to size. If size is greater than the current size, elements are added to the end. If size is less than the current size, elements are removed from the end. If the value type is a primitive type (e.g., char, int, float) or a pointer type (e.g., QWidget *), new elements are not initialized. For other types, the elements are initialized with a default-constructed value. Смотрите также size(). int QVarLengthArray::size () constReturns the number of elements in the array. Смотрите также isEmpty() и resize(). T QVarLengthArray::value ( int i ) constReturns the value at index position i. Если индекс i находится за границами, то функция возвращает значение, создаваемое по умолчанию. Если вы уверены, что i находится в допустимых границах, то вы можете использовать вместо этого at(), которая немного быстрее. Смотрите также at() и operator[](). T QVarLengthArray::value ( int i, const T & defaultValue ) constЭто перегруженная функция. Если индекс i находится за границами, то функция возвращает defaultValue. QVarLengthArray<T, Prealloc> & QVarLengthArray::operator= ( const QVarLengthArray<T, Prealloc> & other )Assigns other to this array and returns a reference to this array. T & QVarLengthArray::operator[] ( int i )Returns a reference to the item at index position i. i must be a valid index position in the array (i.e., 0 <= i < size()). const T & QVarLengthArray::operator[] ( int i ) constЭто перегруженная функция. |
Попытка перевода Qt документации. Если есть желание присоединиться, или если есть замечания или пожелания, то заходите на форум: Перевод Qt документации на русский язык... Люди внесшие вклад в перевод: Команда переводчиков |