📄 qvaluevector.3qt
字号:
.br vec2[10] = "Dave"; // OK.br.fi.PPWhenever inserting, removing or referencing elements in a vector, always make sure you are referring to valid positions. For example:.PP.nf.br void func( QValueVector<int>& vec ).br {.br if ( vec.size() > 10 ) {.br vec[9] = 99; // OK.br }.br };.br.fi.PPThe iterators provided by vector are random access iterators, therefore you can use them with many generic algorithms, for example, algorithms provided by the STL or the QTL..PPAnother way to find an element in the vector is by using the std::find() or qFind() algorithms. For example:.PP.nf.br QValueVector<int> vec;.br ....br QValueVector<int>::const_iterator it = qFind( vec.begin(), vec.end(), 3 );.br if ( it != vector.end() ).br // 'it' points to the found element.br.fi.PPIt is safe to have multiple iterators on the vector at the same time. Since QValueVector manages memory dynamically, all iterators can become invalid if a memory reallocation occurs. For example, if some member of the vector is removed, iterators that point to the removed element and to all following elements become invalidated. Inserting into the middle of the vector will invalidate all iterators. For convenience, the function back() returns a reference to the last element in the vector, and front() returns a reference to the first element. If the vector is empty(), both back() and front() have undefined behavior (your application will crash or do unpredictable things). Use back() and front() with caution, for example:.PP.nf.br QValueVector<int> vec( 3 );.br vec.push_back( 1 );.br vec.push_back( 2 );.br vec.push_back( 3 );.br ....br if ( !vec.empty() ) {.br // OK: modify the first element.br int& i = vec.front();.br i = 18;.br }.br ....br QValueVector<double> dvec;.br double d = dvec.back(); // undefined behavior.br.fi.PPBecause QValueVector manages memory dynamically, it is recommended that you contruct a vector with an initial size. Inserting and removing elements happens fastest when:.TPInserting or removing elements happens at the end() of the vector;.TPThe vector does not need to allocate additional memory..PPBy creating a QValueVector with a sufficiently large initial size, there will be less memory allocations. Do not use an initial size that is too big, since it will still take time to construct all the empty entries, and the extra space will be wasted if it is never used..PPBecause QValueVector is value-based there is no need to be careful about deleting elements in the vector. The vector holds its own copies and will free them if the corresponding member or the vector itself is deleted. You can force the vector to free all of its items with clear()..PPQValueVector is shared implicitly, which means it can be copied in constant time. If multiple QValueVector instances share the same data and one needs to modify its contents, this modifying instance makes a copy and modifies its private copy; it thus does not affect the other instances. This is often called "copy on write". If a QValueVector is being used in a multi-threaded program, you must protect all access to the vector. See QMutex..PPThere are several ways to insert elements into the vector. The push_back() function insert elements into the end of the vector, and is usually fastest. The insert() function can be used to add elements at specific positions within the vector..PPItems can be also be removed from the vector in several ways. There are several variants of the erase() function which removes a specific element, or range of elements, from the vector..PPVectors can be also sorted with various STL algorithms , or it can be sorted using the Qt Template Library. For example with qHeapSort():.PPExample:.PP.nf.br QValueVector<int> v( 4 );.br v.push_back( 5 );.br v.push_back( 8 );.br v.push_back( 3 );.br v.push_back( 4 );.br qHeapSort( v );.br.fi.PPQValueVector stores its elements in contiguous memory. This means that you can use a QValueVector in any situation that requires an array..PPSee also Qt Template Library Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes..SS "Member Type Documentation".SH "QValueVector::ConstIterator"The vector's const iterator type..SH "QValueVector::Iterator"The vector's iterator type..SH "QValueVector::ValueType"The type of the object stored in the vector..SH "QValueVector::const_iterator"The vector's const iterator type..SH "QValueVector::const_pointer"The const pointer to T type..SH "QValueVector::const_reference"The const reference to T type..SH "QValueVector::difference_type"A signed integral type used to represent the distance between two iterators..SH "QValueVector::iterator"The vector's iterator type..SH "QValueVector::pointer"The pointer to T type..SH "QValueVector::reference"The reference to T type..SH "QValueVector::size_type"An unsigned integral type, used to represent various sizes..SH "QValueVector::value_type"The type of the object stored in the vector..SH MEMBER FUNCTION DOCUMENTATION.SH "QValueVector::QValueVector ()"Constructs an empty vector without any elements. To create a vector which reserves an initial amount of space for elements, use \fCQValueVector(size_type n)\fR..SH "QValueVector::QValueVector ( const QValueVector<T> & v )"Constructs a copy of \fIv\fR..PPThis operation costs O(1) time because QValueVector is implicitly shared..PPThe first modification to the vector does takes O(n) time, because the elements must be copied..SH "QValueVector::QValueVector ( size_type n, const T & val = T ( ) )"Constructs a vector with an initial size of \fIn\fR elements. Each element is initialized with the value of \fIval\fR..SH "QValueVector::QValueVector ( std::vector<T> & v )"Constructs a copy of \fIv\fR..SH "QValueVector::QValueVector ( const std::vector<T> & v )"This operation costs O(n) time because \fIv\fR is copied..SH "QValueVector::~QValueVector ()"Destroys the vector, destroying all elements and freeing the allocated memory. References to the values in the vector and all iterators of this vector become invalidated. Note that it is impossible for an iterator to check whether or not it is valid: QValueVector is tuned for performance, not for error checking..SH "void QValueVector::append ( const T & x )"Appends a copy of \fIx\fR to the end of the vector..PPSee also push_back() and insert()..SH "reference QValueVector::at ( size_type i, bool * ok = 0 )"Returns a reference to the element with index \fIi\fR. If \fIok\fR is non-null, and the index \fIi\fR is out of range, *\fIok\fR is set to FALSE and the returned reference is undefined. If the index \fIi\fR is within the range of the vector, and \fIok\fR is non-null, *\fIok\fR is set to TRUE and the returned reference is well defined..SH "const_reference QValueVector::at ( size_type i, bool * ok = 0 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const reference to the element with index \fIi\fR. If \fIok\fR is non-null, and the index \fIi\fR is out of range, *\fIok\fR is set to FALSE and the returned reference is undefined. If the index \fIi\fR is within the range of the vector, and \fIok\fR is non-null, *\fIok\fR is set to TRUE and the returned reference is well defined..SH "reference QValueVector::back ()"Returns a reference to the last element in the vector. If there is no last element, this function has undefined behavior..PPSee also empty() and front()..SH "const_reference QValueVector::back () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const reference to the last element in the vector. If there is no last element, this function has undefined behavior..PPSee also empty() and front()..SH "iterator QValueVector::begin ()"Returns an iterator pointing to the beginning of the vector. If the vector is empty(), the returned iterator will equal end()..SH "const_iterator QValueVector::begin () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const iterator pointing to the beginning of the vector. If the vector is empty(), the returned iterator will equal end()..SH "size_type QValueVector::capacity () const"Returns the maximum number of elements that can be stored in the vector without forcing memory reallocation. If memory reallocation takes place, some or all iterators may become invalidated..SH "void QValueVector::clear ()"Removes all the elements from the vector..SH "size_type QValueVector::count () const"Returns the number of items in the vector..PPSee also isEmpty()..SH "bool QValueVector::empty () const"Returns TRUE if the vector is empty; otherwise returns FALSE. Equivalent to size()==0, only faster..PPThis function is provided for STL compatibility. It is equivalent to isEmpty()..PPSee also size()..SH "iterator QValueVector::end ()"Returns an iterator pointing behind the last element of the vector..SH "const_iterator QValueVector::end () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const iterator pointing behind the last element of the vector..SH "iterator QValueVector::erase ( iterator pos )"Removes the element at position \fIpos\fR and returns the position of the next element..SH "iterator QValueVector::erase ( iterator first, iterator last )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPRemoves all elements from \fIfirst\fR up to but not including \fIlast\fR and returns the position of the next element..SH "reference QValueVector::first ()"Returns a reference to the first item in the vector. If there is no first item, this function has undefined behavior..PPSee also empty() and last()..SH "const_reference QValueVector::first () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..SH "reference QValueVector::front ()"Returns a reference to the first element in the vector. If there is no first element, this function has undefined behavior..PPSee also empty() and back()..SH "const_reference QValueVector::front () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const reference to the first element in the vector. If there is no first element, this function has undefined behavior..PPSee also empty() and back()..SH "iterator QValueVector::insert ( iterator pos, const T & x )"Inserts a copy of \fIx\fR at the position immediately before \fIpos\fR..PPSee also push_back()..SH "iterator QValueVector::insert ( iterator pos, size_type n, const T & x )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPInserts \fIn\fR copies of \fIx\fR immediately before position x..PPSee also push_back()..SH "bool QValueVector::isEmpty () const"Returns TRUE if the vector is empty; returns FALSE otherwise..PPSee also count()..SH "reference QValueVector::last ()"Returns a reference to the last item in the vector. If there is no last item, this function has undefined behavior..PPSee also empty() and first()..SH "const_reference QValueVector::last () const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..SH "QValueVector<T> & QValueVector::operator= ( const QValueVector<T> & v )"Assigns \fIv\fR to this vector and returns a reference to this vector..PPAll iterators of the current vector become invalidated by this operation. The cost of such an assignment is O(1) since QValueVector is implicitly shared..SH "QValueVector<T> & QValueVector::operator= ( const std::vector<T> & v )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAssigns \fIv\fR to this vector and returns a reference to this vector..PPAll iterators of the current vector become invalidated by this operation. The cost of this assignment is O(n) since \fIv\fR is copied..SH "bool QValueVector::operator== ( const QValueVector<T> & x ) const"Returns TRUE if each element in this vector equals each corresponding element in \fIx\fR; otherwise returns FALSE..SH "bool QValueVector::operator== ( const QValueVector<T> & x )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns TRUE if each element in this vector equals each corresponding element in \fIx\fR; otherwise returns FALSE..SH "reference QValueVector::operator[] ( size_type i )"Returns a reference to the element at index \fIi\fR. If \fIi\fR is out of range, this function has undefined behavior..PPSee also at()..SH "const_reference QValueVector::operator[] ( size_type i ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns a const reference to the element at index \fIi\fR. If \fIi\fR is out of range, this function has undefined behavior..PPSee also at()..SH "void QValueVector::pop_back ()"Removes the last item from the vector..PPThis function is provided for STL compatibility..SH "void QValueVector::push_back ( const T & x )"Appends a copy of \fIx\fR to the end of the vector. This is the fastest way to add new elements..PPThis function is provided for STL compatibility. It is equivalent to append()..PPSee also insert()..SH "void QValueVector::reserve ( size_type n )"Increases the vector's capacity. If \fIn\fR is less than or equal to capacity(), nothing happens. Otherwise, additional memory is allocated so that capacity() will be increased to a value greater than or equal to \fIn\fR. All iterators will then become invalidated. Note that the vector's size() and the values of existing elements remain unchanged..SH "void QValueVector::resize ( size_type n, const T & val = T ( ) )"Changes the size of the vector to \fIn\fR. If \fIn\fR is greater than the current size(), elements are added to the end and initialized with the value of \fIval\fR. If \fIn\fR is less than size(), elements are removed from the end. If \fIn\fR is equal to size() nothing happens..SH "size_type QValueVector::size () const"Returns the number of elements in the vector..PPThis function is provided for STL compatibility. It is equivalent to count()..PPSee also empty()..SH "SEE ALSO".BR http://doc.trolltech.com/qvaluevector.html.BR http://www.trolltech.com/faq/tech.html.SH COPYRIGHTCopyright 1992-2001 Trolltech AS, http://www.trolltech.com. See thelicense file included in the distribution for a complete licensestatement..SH AUTHORGenerated automatically from the source code..SH BUGSIf you find a bug in Qt, please report it as described in.BR http://doc.trolltech.com/bughowto.html .Good bug reports help us to help you. Thank you..PThe definitive Qt documentation is provided in HTML format; it islocated at $QTDIR/doc/html and can be read using Qt Assistant or witha web browser. This man page is provided as a convenience for thoseusers who prefer man pages, although this format is not officiallysupported by Trolltech. .PIf you find errors in this manual page, please report them to.BR qt-bugs@trolltech.com .Please include the name of the manual page (qvaluevector.3qt) and the Qtversion (3.1.1).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -