![]() QVector's value type must be an assignable data type. Note that using non-const operators and functions can cause QVector to do a deep copy of the data. You can also call capacity() to find out how much memory QVector actually allocated. 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. QVector tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs. ![]() If the new size is larger than the old size, QVector might need to reallocate the whole vector. Unlike plain C++ arrays, QVectors can be resized at any time by calling resize(). If you want a container class that provides fast insertion/removal in the middle, use QList or QLinkedList instead. With the exception of append() and replace(), these functions can be slow ( linear time) for large vectors, because they require moving many items in the vector by one position in memory. QVector provides these basic functions to add, move, and remove items: insert(), replace(), remove(), prepend(), append(). If you want to find out how many times a particular value occurs in the vector, use count(). If you simply want to check whether a vector contains a particular value, use contains(). If you need a real linked list, which guarantees constant time insertions mid-list and uses iterators to items rather than indexes, use QLinkedList.Ĭout However, QList is used throughout the Qt APIs for passing parameters and for returning values.See the Pros and Cons of Using QList for an explanation. QVector will usually give better performance than QList, because QVector always stores its items sequentially in memory, where QList will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. QVector should be your default first choice.They are often interchangeable, but there are performance consequences. ![]() QList, QLinkedList, QVector, and QVarLengthArray provide similar APIs and functionality. It stores its items in adjacent memory locations and provides fast index-based access. QVector is one of Qt's generic container classes. Value(int i, const T & defaultValue) const LastIndexOf(const T & value, int from = -1) const Insert(QVector::iterator before, T & value) Insert(QVector::iterator before, const T & value) Insert(QVector::iterator before, int count, const T & value) Insert(int i, int count, const T & value) IndexOf(const T & value, int from = 0) const QVector(InputIterator first, InputIterator last)Įrase(QVector::iterator begin, QVector::iterator end)
0 Comments
Leave a Reply. |