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
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
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
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
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.
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.