QValueVector(3qt)
๐ NAME ๐
QValueVector โ a value-based template class that provides a dynamic array. yes bestie, it's giving "resizable array but make it Qt", and this is the QValueVector(3qt) page from the Qt manual, section 3qt. we're in part 1, so buckle up ๐
โจ SYNOPSIS โจ
okay so first, the important vibe check: All the functions in this class are reentrant when Qt is built with thread support.</p>
then you pull it in like this:
#include <qvaluevector.h>
๐ธ Public Members ๐ธ
here's the whole public members roster, no skips, no cuts, fr fr:
typedef T value_typetypedef value_type * pointertypedef const value_type * const_pointertypedef value_type * iteratortypedef const value_type * const_iteratortypedef value_type & referencetypedef const value_type & const_referencetypedef size_t size_typetypedef ptrdiff_t difference_typeQValueVector ()QValueVector ( const QValueVector<T> & v )QValueVector ( size_type n, const T & val = T ( ) )QValueVector ( std::vector<T> & v )QValueVector ( const std::vector<T> & v )~QValueVector ()QValueVector<T> & operator= ( const QValueVector<T> & v )QValueVector<T> & operator= ( const std::vector<T> & v )size_type size () constbool empty () constsize_type capacity () constiterator begin ()const_iterator begin () constconst_iterator constBegin () constiterator end ()const_iterator end () constconst_iterator constEnd () constreference at ( size_type i, bool * ok = 0 )const_reference at ( size_type i, bool * ok = 0 ) constreference operator[] ( size_type i )const_reference operator[] ( size_type i ) constreference front ()const_reference front () constreference back ()const_reference back () constvoid push_back ( const T & x )void pop_back ()iterator insert ( iterator pos, const T & x )iterator insert ( iterator pos, size_type n, const T & x )void reserve ( size_type n )void resize ( size_type n, const T & val = T ( ) )void clear ()iterator erase ( iterator pos )iterator erase ( iterator first, iterator last )bool operator== ( const QValueVector<T> & x )bool operator== ( const QValueVector<T> & x ) consttypedef T ValueTypetypedef ValueType * Iteratortypedef const ValueType * ConstIteratorsize_type count () constbool isEmpty () constreference first ()const_reference first () constreference last ()const_reference last () constvoid append ( const T & x )
and that's the whole list for part 1 ๐ part 2 is coming in hot with the rest of the synopsis and description, so don't go anywhere ๐ฅ
โจ DESCRIPTION โจ
QValueVector is literally a value-based template class that gives you a dynamic array. No cap. ๐
QValueVector is Qt's own implementation of an STL-like vector container. Bestie, if the standard vector isn't available on your target platforms, this is your girl. She's part of the Qt Template Library. โจ
QValueVector<T> defines a template instance to create a vector of values that all have the class T. It does not store pointers to the members of the vector โ it holds a copy of every member. So QValueVector is value based, while QPtrList and QDict are pointer based. ๐ซถ
QValueVector contains and manages a collection of objects of type T, and provides random access iterators so the contained objects can be addressed. QValueVector owns the contained elements. For more relaxed ownership semantics, see QPtrCollection and friends, which are pointer-based containers. ๐
Performance tea: QValueVector gives you good performance if you append or remove elements from the end of the vector. If you insert or remove elements from anywhere but the end, performance is very bad. The reason? Elements have to be copied into new positions. ๐ญ
Some classes cannot be used inside a QValueVector โ for example, all classes derived from QObject, and thus all classes that implement widgets. Only values can be used in a QValueVector. To qualify as a value, the class must provide:
- a copy constructor;
- an assignment operator;
- a default constructor, i.e., a constructor that does not take any arguments.
Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases this is sufficient. ๐
QValueVector uses an STL-like syntax to manipulate and address the objects it contains. See this document for more information.
Example:
#include <qvaluevector.h>
#include <qstring.h>
#include <stdio.h>
class Employee
{
public:
Employee(): s(0) {}
Employee( const QString& name, int salary )
: n( name ), s( salary )
{ }
QString name() const { return n; }
int salary() const { return s; }
void setSalary( int salary ) { s = salary; }
private:
QString n;
int s;
};
int main()
{
typedef QValueVector<Employee> EmployeeVector;
EmployeeVector vec( 3 ); // vector of 3 Employees
vec[0] = Employee( "Bill", 50000 );
vec[1] = Employee( "Steve", 80000 );
vec[2] = Employee( "Ron", 60000 );
Employee joe( "Joe", 50000 );
vec.push_back( joe ); // vector expands to accommodate 4 Employees
joe.setSalary( 70000 );
EmployeeVector::iterator it;
for( it = vec.begin(); it != vec.end(); ++it )
printf( "%s earns %d\n", (*it).name().latin1(), (*it).salary() );
return 0;
}
Program output:
Bill earns 50000
Steve earns 80000
Ron earns 60000
Joe earns 50000
As you can see, the most recent change to Joe's salary did not affect the value in the vector because the vector created a copy of Joe's entry. ๐
Many Qt functions return const value vectors; to iterate over these you should make a copy and iterate over the copy.
There are several ways to find items in the vector. The begin() and end() functions return iterators to the beginning and end of the vector. The advantage of getting an iterator is that you can move forward or backward from this position by incrementing/decrementing the iterator. The iterator returned by end() points to the element which is one past the last element in the container. The past-the-end iterator is still associated with the vector it belongs to, however it is not dereferenceable; operator*() will not return a well-defined value. If the vector is empty(), the iterator returned by begin() will equal the iterator returned by end(). ๐
The fastest way to access an element of a vector is by using operator[]. This function provides random access and will return a reference to the element located at the specified index. Thus, you can access every element directly, in constant time, providing you know the location of the element. It is undefined to access an element that does not exist (your application will probably crash). For example:
QValueVector<int> vec1; // an empty vector
vec1[10] = 4; // WARNING: undefined, probably a crash
QValueVector<QString> vec2(25); // initialize with 25 elements
vec2[10] = "Dave"; // OK
Whenever inserting, removing or referencing elements in a vector, always make sure you are referring to valid positions. For example:
void func( QValueVector<int>& vec )
{
if ( vec.size() > 10 ) {
vec[9] = 99; // OK
}
};
The 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. โจ
Another way to find an element in the vector is by using the std::find() or qFind() algorithms. For example:
QValueVector<int> vec;
...
QValueVector<int>::const_iterator it = qFind( vec.begin(), vec.end(), 3 );
if ( it != vector.end() )
// 'it' points to the found element
It 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:
QValueVector<int> vec( 3 );
vec.push_back( 1 );
vec.push_back( 2 );
vec.push_back( 3 );
...
if ( !vec.empty() ) {
// OK: modify the first element
int& i = vec.front();
i = 18;
}
...
QValueVector<double> dvec;
double d = dvec.back(); // undefined behavior
Because QValueVector manages memory dynamically, it is recommended that you contruct a vector with an initial size. Inserting and removing elements happens fastest when:
- Inserting or removing elements happens at the
end()of the vector; - The vector does not need to allocate additional memory.
By 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. ๐ธ
Because 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(). ๐ง
QValueVector 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. ๐ฅ
There 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.
Items 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. ๐
Vectors can be also sorted with various STL algorithms , or it can be sorted using the Qt Template Library. For example with qHeapSort():
Example:
QValueVector<int> v( 4 );
v.push_back( 5 );
v.push_back( 8 );
v.push_back( 3 );
v.push_back( 4 );
qHeapSort( v );
QValueVector stores its elements in contiguous memory. This means that you can use a QValueVector in any situation that requires an array. ๐
See also Qt Template Library Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes.
๐งฌ Member Type Documentation ๐งฌ
QValueVector::ConstIteratorโ the vector's const iterator type.QValueVector::Iteratorโ the vector's iterator type.QValueVector::ValueTypeโ the type of the object stored in the vector.QValueVector::const_iteratorโ the vector's const iterator type.QValueVector::const_pointerโ the const pointer toTtype.QValueVector::const_referenceโ the const reference toTtype.QValueVector::difference_typeโ a signed integral type used to represent the distance between two iterators.QValueVector::iteratorโ the vector's iterator type.QValueVector::pointerโ the pointer toTtype.QValueVector::referenceโ the reference toTtype.QValueVector::size_typeโ an unsigned integral type, used to represent various sizes.QValueVector::value_typeโ the type of the object stored in the vector.
๐ MEMBER FUNCTION DOCUMENTATION ๐
QValueVector::QValueVector ()
Constructs an empty vector without any elements. To create a vector which reserves an initial amount of space for elements, use QValueVector(size_type n). ๐
QValueVector::QValueVector ( const QValueVector<T> & v )
Constructs a copy of v.
This operation costs O(1) time because QValueVector is implicitly shared. โจ
The first modification to the vector does take O(n) time, because the elements must be copied. ๐ญ
QValueVector::QValueVector ( size_type n, const T & val = T ( ) )
Constructs a vector with an initial size of n elements. Each element is initialized with the value of val. ๐
QValueVector::QValueVector ( std::vector<T> & v )
Constructs a copy of v. ๐ซถ
QValueVector::QValueVector ( const std::vector<T> & v )
This operation costs O(n) time because v is copied. ๐
QValueVector::~QValueVector ()
okay so this is the destructor ๐ it destroys the vector, destroys all the elements, and frees the allocated memory. and because everything's gone-gone, references to the values in the vector and all iterators of this vector become invalidated. periodt.
ngl this is the part where you need to pay attention bestie: it is impossible for an iterator to check whether or not it is valid. QValueVector is tuned for performance, not for error checking. so don't go asking your iterator if it's okay, it literally cannot tell you ๐ญ
void QValueVector::append ( const T & x )
appends a copy of x to the end of the vector. that's it, that's the whole vibe ๐
See also push_back() and insert().
reference QValueVector::at ( size_type i, bool * ok = 0 )
returns a reference to the element with index i โจ and here's the tea about ok:
- if
okis non-null and the indexiis out of range โ*okgets set toFALSEand the returned reference is undefined ๐ - if the index
iis within the range of the vector andokis non-null โ*okgets set toTRUEand the returned reference is well defined ๐
const_reference QValueVector::at ( size_type i, bool * ok = 0 ) const
okay so this is an overloaded member function, provided for convenience โจ it behaves essentially like the above function.
returns a const reference to the element with index i. if ok is non-null and the index i is out of range, *ok is set to FALSE and the returned reference is undefined. if the index i is within the range of the vector and ok is non-null, *ok is set to TRUE and the returned reference is well defined. same rules, const flavored ๐ซถ
reference QValueVector::back ()
returns a reference to the last element in the vector. if there is no last element, this function has undefined behavior โ so pls check empty() first bestie ๐ญ
See also empty() and front().
const_reference QValueVector::back () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns a const reference to the last element in the vector. if there is no last element, this function has undefined behavior.
See also empty() and front().
iterator QValueVector::begin ()
returns an iterator pointing to the beginning of the vector ๐
and if the vector is empty(), the returned iterator will equal end().
const_iterator QValueVector::begin () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns a const iterator pointing to the beginning of the vector. if the vector is empty(), the returned iterator will equal end().
size_type QValueVector::capacity () const
returns the max number of elements that can be stored in the vector without forcing memory reallocation. and if memory reallocation does take place? some or all iterators may become invalidated ๐ฌ iconic but dangerous.
void QValueVector::clear ()
removes all the elements from the vector. bye bye everything ๐โจ
const_iterator QValueVector::constBegin () const
returns a const iterator pointing to the beginning of the vector. if the vector is empty(), the returned iterator will equal end().
See also constEnd().
const_iterator QValueVector::constEnd () const
returns a const iterator pointing behind the last element of the vector.
See also constBegin().
size_type QValueVector::count () const
returns the number of items in the vector ๐งฎ
See also isEmpty().
bool QValueVector::empty () const
returns TRUE if the vector is empty; otherwise returns FALSE. equivalent to size()==0, only faster โก
this function is provided for STL compatibility. it is equivalent to isEmpty().
See also size().
iterator QValueVector::end ()
returns an iterator pointing behind the last element of the vector. classic one-past-the-end energy ๐ฆ
const_iterator QValueVector::end () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns a const iterator pointing behind the last element of the vector.
iterator QValueVector::erase ( iterator pos )
removes the element at position pos and returns the position of the next element ๐
iterator QValueVector::erase ( iterator first, iterator last )
overloaded member function, provided for convenience โจ behaves essentially like the above function.
removes all elements from first up to but not including last and returns the position of the next element. yes, last stays. that's the whole point bestie ๐
reference QValueVector::first ()
returns a reference to the first item in the vector. if there is no first item, this function has undefined behavior ๐
See also empty() and last().
const_reference QValueVector::first () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
reference QValueVector::front ()
returns a reference to the first element in the vector. if there is no first element, this function has undefined behavior โ check empty() first, i'm begging ๐ญ
See also empty() and back().
const_reference QValueVector::front () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns a const reference to the first element in the vector. if there is no first element, this function has undefined behavior.
See also empty() and back().
iterator QValueVector::insert ( iterator pos, const T & x )
inserts a copy of x at the position immediately before pos โจ
See also push_back().
iterator QValueVector::insert ( iterator pos, size_type n, const T & x )
overloaded member function, provided for convenience โจ behaves essentially like the above function.
inserts n copies of x immediately before position x.
See also push_back().
bool QValueVector::isEmpty () const
returns TRUE if the vector is empty; returns FALSE otherwise ๐ซถ
See also count().
reference QValueVector::last ()
returns a reference to the last item in the vector. if there is no last item, this function has undefined behavior ๐ฌ
See also empty() and first().
const_reference QValueVector::last () const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
QValueVector<T> & QValueVector::operator= ( const QValueVector<T> & v )
assigns v to this vector and returns a reference to this vector ๐
all iterators of the current vector become invalidated by this operation. the cost of such an assignment is O(1) since QValueVector is implicitly shared. O(1)?? we love a cheap assignment, no cap ๐ฅ
QValueVector<T> & QValueVector::operator= ( const std::vector<T> & v )
overloaded member function, provided for convenience โจ behaves essentially like the above function.
assigns v to this vector and returns a reference to this vector.
all iterators of the current vector become invalidated by this operation. the cost of this assignment is O(n) since v is copied. okay so this one is not the cheap one, because copying is copying ๐ญ
bool QValueVector::operator== ( const QValueVector<T> & x ) const
returns TRUE if each element in this vector equals each corresponding element in x; otherwise returns FALSE โจ element-by-element, one-to-one, matching energy ๐
bool QValueVector::operator== ( const QValueVector<T> & x )
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns TRUE if each element in this vector equals each corresponding element in x; otherwise returns FALSE.
reference QValueVector::operator[] ( size_type i )
returns a reference to the element at index i. if i is out of range, this function has undefined behavior ๐ โ yes, operator[] does NOT check for you. that's at()'s job.
See also at().
const_reference QValueVector::operator[] ( size_type i ) const
overloaded member function, provided for convenience โจ behaves essentially like the above function.
returns a const reference to the element at index i. if i is out of range, this function has undefined behavior.
See also at().
void QValueVector::pop_back ()
removes the last item from the vector ๐
this function is provided for STL compatibility.
void QValueVector::push_back ( const T & x )
appends a copy of x to the end of the vector. this is the fastest way to add new elements โก
this function is provided for STL compatibility. it is equivalent to append().
See also insert().
void QValueVector::reserve ( size_type n )
increases the vector's capacity. here's the breakdown bestie:
- if
nis less than or equal tocapacity(), nothing happens ๐คทโโ๏ธ - otherwise, additional memory is allocated so that
capacity()will be increased to a value greater than or equal ton. all iterators will then become invalidated ๐
and note: the vector's size() and the values of existing elements remain unchanged. capacity grows, contents stay put ๐
void QValueVector::resize ( size_type n, const T & val = T ( ) )
changes the size of the vector to n. the rules:
- if
nis greater than the currentsize(), elements are added to the end and initialized with the value ofvalโจ - if
nis less thansize(), elements are removed from the end ๐ - if
nis equal tosize(), nothing happens ๐
size_type QValueVector::size () const
returns the number of elements in the vector ๐งฎ
this function is provided for STL compatibility. it is equivalent to count().
See also empty().
SEE ALSO ๐
COPYRIGHT ๐
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
AUTHOR ๐
Generated automatically from the source code. (so nobody can take credit for this masterpiece, it's the source code's moment ๐ )
BUGS ๐
if you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. good bug reports help us to help you. thank you ๐ซถ
the definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. this man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. (we're the unofficial cute version, iykyk ๐ธ)
if you find errors in this manual page, please report them to qt-bugs@trolltech.com. please include the name of the manual page (qvaluevector.3qt) and the Qt version (3.3.8) ๐
and that's a wrap on QValueVector(3qt) bestie!! ๐ฆโจ this whole thing came from Trolltech AS, dated 2 February 2007, and it's the QValueVector(3qt) page โ go forth and vector responsibly, mwah ๐๐
๐ฆ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 ยท QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 ยท QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms