source: trunk/src/qt3support/tools/q3valuevector.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 10.4 KB
RevLine 
[556]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[556]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
[846]9** $QT_BEGIN_LICENSE:FDL$
[556]10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
[846]13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
[556]15**
[846]16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
[556]21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \class Q3ValueVector
30 \brief The Q3ValueVector class is a value-based template class that provides a dynamic array.
31 \compat
32
33 Q3ValueVector is a Qt implementation of an STL-like vector
34 container. It can be used in your application if the standard \c
35 vector is not available for your target platforms.
36
37 Q3ValueVector\<T\> defines a template instance to create a vector
38 of values that all have the class T. Q3ValueVector does not store
39 pointers to the members of the vector; it holds a copy of every
40 member. Q3ValueVector is said to be value based; in contrast,
41 Q3PtrList and Q3Dict are pointer based.
42
43 Q3ValueVector contains and manages a collection of objects of type
44 T and provides random access iterators that allow the contained
45 objects to be addressed. Q3ValueVector owns the contained
46 elements. For more relaxed ownership semantics, see Q3PtrCollection
47 and friends, which are pointer-based containers.
48
49 Q3ValueVector provides good performance if you append or remove
50 elements from the end of the vector. If you insert or remove
51 elements from anywhere but the end, performance is very bad. The
52 reason for this is that elements must to be copied into new
53 positions.
54
55 Some classes cannot be used within a Q3ValueVector: for example,
56 all classes derived from QObject and thus all classes that
57 implement widgets. Only values can be used in a Q3ValueVector. To
58 qualify as a value the class must provide:
59 \list
60 \i a copy constructor;
61 \i an assignment operator;
62 \i a default constructor, i.e., a constructor that does not take any arguments.
63 \endlist
64
65 Note that C++ defaults to field-by-field assignment operators and
66 copy constructors if no explicit version is supplied. In many
67 cases this is sufficient.
68
69 Q3ValueVector uses an STL-like syntax to manipulate and address the
70 objects it contains.
71
72 Example:
73 \snippet doc/src/snippets/code/doc_src_q3valuevector.qdoc 0
74
75 Program output:
76 \snippet doc/src/snippets/code/doc_src_q3valuevector.qdoc 1
77
78 As you can see, the most recent change to Joe's salary did not
79 affect the value in the vector because the vector created a copy
80 of Joe's entry.
81
82 Many Qt functions return const value vectors; to iterate over
83 these you should make a copy and iterate over the copy.
84
85 There are several ways to find items in the vector. The begin()
86 and end() functions return iterators to the beginning and end of
87 the vector. The advantage of getting an iterator is that you can
88 move forward or backward from this position by
89 incrementing/decrementing the iterator. The iterator returned by
90 end() points to the element which is one past the last element in
91 the container. The past-the-end iterator is still associated with
92 the vector it belongs to, however it is \e not dereferenceable;
93 operator*() will not return a well-defined value. If the vector is
94 empty(), the iterator returned by begin() will equal the iterator
95 returned by end().
96
97 The fastest way to access an element of a vector is by using
98 operator[]. This function provides random access and will return
99 a reference to the element located at the specified index. Thus,
100 you can access every element directly, in constant time, providing
101 you know the location of the element. It is undefined to access
102 an element that does not exist (your application will probably
103 crash). For example:
104
105 \snippet doc/src/snippets/code/doc_src_q3valuevector.qdoc 2
106
107 Whenever inserting, removing or referencing elements in a vector,
108 always make sure you are referring to valid positions. For
109 example:
110
111 \snippet doc/src/snippets/code/doc_src_q3valuevector.qdoc 3
112
113 The iterators provided by vector are random access iterators,
114 therefore you can use them with many generic algorithms, for
115 example, algorithms provided by the STL.
116
117 It is safe to have multiple iterators on the vector at the same
118 time. Since Q3ValueVector manages memory dynamically, all iterators
119 can become invalid if a memory reallocation occurs. For example,
120 if some member of the vector is removed, iterators that point to
121 the removed element and to all following elements become
122 invalidated. Inserting into the middle of the vector will
123 invalidate all iterators. For convenience, the function back()
124 returns a reference to the last element in the vector, and front()
125 returns a reference to the first element. If the vector is
126 empty(), both back() and front() have undefined behavior (your
127 application will crash or do unpredictable things). Use back() and
128 front() with caution, for example:
129
130 \snippet doc/src/snippets/code/doc_src_q3valuevector.qdoc 4
131
132 Because Q3ValueVector manages memory dynamically, it is recommended
133 that you contruct a vector with an initial size. Inserting and
134 removing elements happens fastest when:
135 \list
136 \i Inserting or removing elements happens at the end() of the
137 vector;
138 \i The vector does not need to allocate additional memory.
139 \endlist
140
141 By creating a Q3ValueVector with a sufficiently large initial size,
142 there will be less memory allocations. Do not use an initial size
143 that is too big, since it will still take time to construct all
144 the empty entries, and the extra space will be wasted if it is
145 never used.
146
147 Because Q3ValueVector is value-based there is no need to be careful
148 about deleting elements in the vector. The vector holds its own
149 copies and will free them if the corresponding member or the
150 vector itself is deleted. You can force the vector to free all of
151 its items with clear().
152
153 Q3ValueVector is shared implicitly, which means it can be copied in
154 constant time. If multiple Q3ValueVector instances share the same
155 data and one needs to modify its contents, this modifying instance
156 makes a copy and modifies its private copy; it thus does not
157 affect the other instances. This is often called "copy on write".
158 If a Q3ValueVector is being used in a multi-threaded program, you
159 must protect all access to the vector. See QMutex.
160
161 There are several ways to insert elements into the vector. The
162 push_back() function insert elements into the end of the vector,
163 and is usually fastest. The insert() function can be used to add
164 elements at specific positions within the vector.
165
166 Items can be also be removed from the vector in several ways.
167 There are several variants of the erase() function which removes a
168 specific element, or range of elements, from the vector.
169
170 Q3ValueVector stores its elements in contiguous memory. This means
171 that you can use a Q3ValueVector in any situation that requires an
172 array.
173*/
174
175/*!
176 \fn Q3ValueVector::Q3ValueVector()
177
178 Constructs an empty vector without any elements. To create a
179 vector which reserves an initial amount of space for elements, use
180 \c Q3ValueVector(size_type n).
181*/
182
183/*!
184 \fn Q3ValueVector::Q3ValueVector( const Q3ValueVector<T>& v )
185
186 Constructs a copy of \a v.
187
188 This operation costs O(1) time because Q3ValueVector is implicitly
189 shared.
190
191 The first modification to the vector does takes O(n) time, because
192 the elements must be copied.
193*/
194
195/*!
196 \fn Q3ValueVector::Q3ValueVector( const std::vector<T>& v )
197
198 This operation costs O(n) time because \a v is copied.
199*/
200
201/*!
202 \fn Q3ValueVector::Q3ValueVector( QVector<T>::size_type n, const T& val )
203
204 Constructs a vector with an initial size of \a n elements. Each
205 element is initialized with the value of \a val.
206*/
207
208/*!
209 \fn Q3ValueVector<T>& Q3ValueVector::operator=( const Q3ValueVector<T>& v )
210
211 Assigns \a v to this vector and returns a reference to this vector.
212
213 All iterators of the current vector become invalidated by this
214 operation. The cost of such an assignment is O(1) since
215 Q3ValueVector is implicitly shared.
216*/
217
218/*!
219 \fn Q3ValueVector<T>& Q3ValueVector::operator=( const std::vector<T>& v )
220
221 \overload
222
223 Assigns \a v to this vector and returns a reference to this vector.
224
225 All iterators of the current vector become invalidated by this
226 operation. The cost of this assignment is O(n) since \a v is
227 copied.
228*/
229
230/*!
231 \fn T &Q3ValueVector::at( int i , bool* ok )
232
233 Returns a reference to the element with index \a i. If \a ok is
234 non-null, and the index \a i is out of range, *\a ok is set to
235 FALSE and the returned reference is undefined. If the index \a i
236 is within the range of the vector, and \a ok is non-null, *\a ok
237 is set to TRUE and the returned reference is well defined.
238*/
239
240/*!
241 \fn const T &Q3ValueVector::at( int i , bool* ok ) const
242
243 \overload
244
245 Returns a const reference to the element with index \a i. If \a ok
246 is non-null, and the index \a i is out of range, *\a ok is set to
247 FALSE and the returned reference is undefined. If the index \a i
248 is within the range of the vector, and \a ok is non-null, *\a ok
249 is set to TRUE and the returned reference is well defined.
250*/
251
252/*!
253 \fn void Q3ValueVector::resize( int n, const T& val = T() )
254
255 Changes the size of the vector to \a n. If \a n is greater than
256 the current size(), elements are added to the end and initialized
257 with the value of \a val. If \a n is less than size(), elements
258 are removed from the end. If \a n is equal to size() nothing
259 happens.
260*/
Note: See TracBrowser for help on using the repository browser.