1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/qalgorithms.qdoc -->
7
<title>Qt 4.0: <QtAlgorithms> - Generic Algorithms</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center"><QtAlgorithms> - Generic Algorithms</h1>
21
<p>The <QtAlgorithms> header file provides generic template-based algorithms. <a href="#details">More...</a></p>
22
<a name="Functionsx"></a>
25
<li><div class="fn"/>RandomAccessIterator <b><a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a></b> ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</li>
26
<li><div class="fn"/>OutputIterator <b><a href="qtalgorithms.html#qCopy">qCopy</a></b> ( InputIterator <i>begin1</i>, InputIterator <i>end1</i>, OutputIterator <i>begin2</i> )</li>
27
<li><div class="fn"/>BiIterator2 <b><a href="qtalgorithms.html#qCopyBackward">qCopyBackward</a></b> ( BiIterator1 <i>begin1</i>, BiIterator1 <i>end1</i>, BiIterator2 <i>end2</i> )</li>
28
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qCount">qCount</a></b> ( InputIterator <i>begin</i>, InputIterator <i>end</i>, const T & <i>value</i>, Size & <i>n</i> )</li>
29
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qDeleteAll">qDeleteAll</a></b> ( ForwardIterator <i>begin</i>, ForwardIterator <i>end</i> )</li>
30
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qDeleteAll-2">qDeleteAll</a></b> ( const Container & <i>c</i> )</li>
31
<li><div class="fn"/>bool <b><a href="qtalgorithms.html#qEqual">qEqual</a></b> ( InputIterator1 <i>begin1</i>, InputIterator1 <i>end1</i>, InputIterator2 <i>begin2</i> )</li>
32
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qFill">qFill</a></b> ( ForwardIterator <i>begin</i>, ForwardIterator <i>end</i>, const T & <i>value</i> )</li>
33
<li><div class="fn"/>InputIterator <b><a href="qtalgorithms.html#qFind">qFind</a></b> ( InputIterator <i>begin</i>, InputIterator <i>end</i>, const T & <i>value</i> )</li>
34
<li><div class="fn"/>RandomAccessIterator <b><a href="qtalgorithms.html#qLowerBound">qLowerBound</a></b> ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</li>
35
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qSort">qSort</a></b> ( BiIterator <i>begin</i>, BiIterator <i>end</i> )</li>
36
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qSort-2">qSort</a></b> ( BiIterator <i>begin</i>, BiIterator <i>end</i>, LessThan <i>lessThan</i> )</li>
37
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qSort-3">qSort</a></b> ( Container & <i>container</i> )</li>
38
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qStableSort">qStableSort</a></b> ( BiIterator <i>begin</i>, BiIterator <i>end</i> )</li>
39
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qStableSort-2">qStableSort</a></b> ( BiIterator <i>begin</i>, BiIterator <i>end</i>, LessThan <i>lessThan</i> )</li>
40
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qStableSort-3">qStableSort</a></b> ( Container & <i>container</i> )</li>
41
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qSwap">qSwap</a></b> ( T & <i>var1</i>, T & <i>var2</i> )</li>
42
<li><div class="fn"/>RandomAccessIterator <b><a href="qtalgorithms.html#qUpperBound">qUpperBound</a></b> ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</li>
44
<a name="details"></a>
46
<h2>Detailed Description</h2>
47
<p>The <QtAlgorithms> header file provides generic template-based algorithms.</p>
48
<p>Qt provides a number of global template functions in <tt><QtAlgorithms></tt> that work on containers and perform well-know algorithms. You can use these algorithms with any <a href="containers.html#container-class">container class</a> that provides STL-style iterators, including Qt's <a href="qlist.html">QList</a>, <a href="qlinkedlist.html">QLinkedList</a>, <a href="qvector.html">QVector</a>, <a href="qmap.html">QMap</a>, and <a href="qhash.html">QHash</a> classes.</p>
49
<p>These functions have taken their inspiration from similar functions available in the STL <tt><algorithm></tt> header. Most of them have a direct STL equivalent; for example, <a href="qtalgorithms.html#qCopyBackward">qCopyBackward</a>() is the same as STL's copy_backward() algorithm.</p>
50
<p>If STL is available on all your target platforms, you can use the STL algorithms instead of their Qt counterparts. One reason why you might want to use the the STL algorithms is that STL provides dozens and dozens of algorithms, whereas Qt only provides the most important ones, making no attempt to duplicate functionality that is already provided by the C++ standard.</p>
51
<p>Most algorithms take <a href="containers.html#stl-style-iterators">STL-style iterators</a> as parameters. The algorithms are generic in the sense that they aren't bound to a specific iterator class; you can use them with any iterators that meet a certain set of requirements.</p>
52
<p>Let's take the <a href="qtalgorithms.html#qFill">qFill</a>() algorithm as an example. Unlike <a href="qvector.html">QVector</a>, <a href="qlist.html">QList</a> has no fill() function that can be used to fill a list with a particular value. If you need that functionality, you can use <a href="qtalgorithms.html#qFill">qFill</a>():</p>
53
<pre> QList<QString> list;
54
list << "one" << "two" << "three";
56
qFill(list.begin(), list.end(), "eleven");
57
// list: [ "eleven", "eleven", "eleven" ]</pre>
58
<p><a href="qtalgorithms.html#qFill">qFill</a>() takes a begin iterator, an end iterator, and a value. In the example above, we pass <tt>list.begin()</tt> and <tt>list.end()</tt> as the begin and end iterators, but this doesn't have to be the case:</p>
59
<pre> qFill(list.begin() + 1, list.end(), "six");
60
// list: [ "eleven", "six", "six" ]</pre>
61
<p>The various algorithms have different requirements for the iterators they accept. For example, <a href="qtalgorithms.html#qFill">qFill</a>() accepts two <a href="#input-iterators">input iterators</a>, the most minimal requirement for an iterator type. The requirements are specified for every algorithm. If an iterator of the wrong type is passed (for example, <a href="qlist.html#ConstIterator-typedef">QList::ConstIterator</a> is passed as an <a href="#output-iterators">output iterator</a>), you will always get a compiler error, although not necessarily a very informative one.</p>
62
<p>Some algorithms have special requirements on the value type stored in the containers. For example, <a href="qtalgorithms.html#qEqual">qEqual</a>() requires that the value type supports operator==(), which it uses to compare items. Similarly, <a href="qtalgorithms.html#qDeleteAll">qDeleteAll</a>() requires that the value type is a non-const pointer type (for example, <a href="qwidget.html">QWidget</a> *). The value type requirements are specified for each algorithm, and the compiler will produce an error if a requirement isn't met.</p>
63
<a name="binaryfind-example"></a><p>The generic algorithms can be used on other container classes than those provided by Qt and STL. The syntax of STL-style iterators is modeled after C++ pointers, so it's possible to use plain arrays as containers and plain pointers as iterators. A common idiom is to use <a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a>() together with two static arrays: one that contains a list of keys, and another that contains a list of associated values. For example, the following code will look up an HTML entity (e.g., <tt>&amp</tt>;) in the <tt>name_table</tt> array and return the corresponding Unicode value from the <tt>value_table</tt> if the entity is recognized:</p>
64
<pre> QChar resolveEntity(const QString &entity)
66
static const QLatin1String name_table[] = {
67
"AElig", "Aacute", ..., "zwnj"
69
static const ushort value_table[] = {
70
0x0061, 0x00c1, ..., 0x200c
72
int N = sizeof(name_table) / sizeof(name_table[0]);
74
const QLatin1String *name = qBinaryFind(name_table, name_table + N,
76
int index = name - name_table;
80
return QChar(value_table[index]);
82
<p>This kind of code is for advanced users only; for most applications, a <a href="qmap.html">QMap</a>- or <a href="qhash.html">QHash</a>-based approach would work just as well:</p>
83
<pre> QChar resolveEntity(const QString &entity)
85
static QMap<QString, int> entityMap;
88
entityMap.insert("AElig", 0x0061);
89
entityMap.insert("Aacute", 0x00c1);
91
entityMap.insert("zwnj", 0x200c);
93
return QChar(entityMap.value(entity));
95
<a name="types-of-iterators"></a>
96
<h3>Types of Iterators</h3>
97
<p>The algorithms have certain requirements on the iterator types they accept, and these are specified individually for each function. The compiler will produce an error if a requirement isn't met.</p>
98
<a name="input-iterators"></a>
99
<h4>Input Iterators</h4>
100
<p>An <i>input iterator</i> is an iterator that can be used for reading data sequentially from a container. It must provide the following operators: <tt>==</tt> and <tt>!=</tt> for comparing two iterators, unary <tt>*</tt> for retrieving the value stored in the item, and prefix <tt>++</tt> for advancing to the next item.</p>
101
<p>The Qt containers' iterator types (const and non-const) are all input iterators.</p>
102
<a name="output-iterators"></a>
103
<h4>Output Iterators</h4>
104
<p>An <i>output iterator</i> is an iterator that can be used for writing data sequentially to a container or to some output stream. It must provide the following operators: unary <tt>*</tt> for writing a value (i.e., <tt>*it = val</tt>) and prefix <tt>++</tt> for advancing to the next item.</p>
105
<p>The Qt containers' non-const iterator types are all output iterators.</p>
106
<a name="forward-iterators"></a>
107
<h4>Forward Iterators</h4>
108
<p>A <i>forward iterator</i> is an iterator that meets the requirements of both input iterators and output iterators.</p>
109
<p>The Qt containers' non-const iterator types are all forward iterators.</p>
110
<a name="bidirectional-iterators"></a>
111
<h4>Bidirectional Iterators</h4>
112
<p>A <i>bidirectional iterator</i> is an iterator that meets the requirements of forward iterators but that in addition supports prefix <tt>--</tt> for iterating backward.</p>
113
<p>The Qt containers' non-const iterator types are all bidirectional iterators.</p>
114
<a name="random-access-iterators"></a>
115
<h4>Random Access Iterators</h4>
116
<p>The last category, <i>random access iterators</i>, is the most powerful type of iterator. It supports all the requirements of a bidirectional iterator, and supports the following operations:</p>
117
<table align="center" cellpadding="2" cellspacing="1" border="0">
118
<tr valign="top" bgcolor="#f0f0f0"><td><tt>i += n</tt></td><td>advances iterator <tt>i</tt> by <tt>n</tt> positions</td></tr>
119
<tr valign="top" bgcolor="#e0e0e0"><td><tt>i -= n</tt></td><td>moves iterator <tt>i</tt> back by <tt>n</tt> positions</td></tr>
120
<tr valign="top" bgcolor="#f0f0f0"><td><tt>i + n</tt> or <tt>n + i</tt></td><td>returns the iterator for the item <tt>n</tt> positions ahead of iterator <tt>i</tt></td></tr>
121
<tr valign="top" bgcolor="#e0e0e0"><td><tt>i - n</tt></td><td>returns the iterator for the item <tt>n</tt> positions behind of iterator <tt>i</tt></td></tr>
122
<tr valign="top" bgcolor="#f0f0f0"><td><tt>i - j</tt></td><td>returns the number of items between iterators <tt>i</tt> and <tt>j</tt></td></tr>
123
<tr valign="top" bgcolor="#e0e0e0"><td><tt>i[n]</tt></td><td>same as <tt>*(i + n)</tt></td></tr>
124
<tr valign="top" bgcolor="#f0f0f0"><td><tt>i < j</tt></td><td>returns true if iterator <tt>j</tt> comes after iterator <tt>i</tt></td></tr>
126
<p><a href="qlist.html">QList</a>, <a href="qlinkedlist.html">QLinkedList</a>, and <a href="qvector.html">QVector</a>'s non-const iterator types are random access iterators.</p>
127
<p>See also <a href="containers.html#container-classes">container classes</a> and <a href="qtglobal.html"><QtGlobal></a>.</p>
129
<h2>Function Documentation</h2>
130
<h3 class="fn"><a name="qBinaryFind"></a>RandomAccessIterator qBinaryFind ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</h3>
131
<p>Performs a binary search of the range [<i>begin</i>, <i>end</i>) and returns the position of an occurrence of <i>value</i>. If there are no occurrences of <i>value</i>, returns <i>end</i>.</p>
132
<p>The items in the range [<i>begin</i>, <i>end</i>) must be sorted in ascending order; see <a href="qtalgorithms.html#qSort">qSort</a>().</p>
133
<p>If there are many occurrences of the same value, any one of them could be returned. Use <a href="qtalgorithms.html#qLowerBound">qLowerBound</a>() or <a href="qtalgorithms.html#qUpperBound">qUpperBound</a>() if you need finer control.</p>
135
<pre> QVector<int> vect;
136
vect << 3 << 3 << 6 << 6 << 6 << 8;
138
QVector<int>::Iterator i =
139
qBinaryFind(vect.begin(), vect.end(), 6);
140
// i == vect.begin() + 2 (or 3 or 4)</pre>
141
<p>This function requires the item type (in the example above, <a href="qstring.html">QString</a>) to implement <tt>operator<()</tt>.</p>
142
<p>See the <a href="qtalgorithms.html#binaryfind-example">detailed description</a> for an example usage.</p>
143
<p>See also <a href="qtalgorithms.html#qLowerBound">qLowerBound</a>(), <a href="qtalgorithms.html#qUpperBound">qUpperBound</a>(), and <a href="qtalgorithms.html#random-access-iterators">random access iterators</a>.</p>
144
<h3 class="fn"><a name="qCopy"></a>OutputIterator qCopy ( InputIterator <i>begin1</i>, InputIterator <i>end1</i>, OutputIterator <i>begin2</i> )</h3>
145
<p>Copies the items from range [<i>begin1</i>, <i>end1</i>) to range [<i>begin2</i>, ...), in the order in which they appear.</p>
146
<p>The item at position <i>begin1</i> is assigned to that at position <i>begin2</i>; the item at position <i>begin1</i> + 1 is assigned to that at position <i>begin2</i> + 1; and so on.</p>
148
<pre> QList<QString> list;
149
list << "one" << "two" << "three";
151
QVector<QString> vect1(3);
152
qCopy(list.begin(), list.end(), vect1.begin());
153
// vect: [ "one", "two", "three" ]
155
QVector<QString> vect2(8);
156
qCopy(list.begin(), list.end(), vect2.begin() + 2);
157
// vect: [ "", "", "one", "two", "three", "", "", "" ]</pre>
158
<p>See also <a href="qtalgorithms.html#qCopyBackward">qCopyBackward</a>(), <a href="qtalgorithms.html#input-iterators">input iterators</a>, and <a href="qtalgorithms.html#output-iterators">output iterators</a>.</p>
159
<h3 class="fn"><a name="qCopyBackward"></a>BiIterator2 qCopyBackward ( BiIterator1 <i>begin1</i>, BiIterator1 <i>end1</i>, BiIterator2 <i>end2</i> )</h3>
160
<p>Copies the items from range [<i>begin1</i>, <i>end1</i>) to range [..., <i>end2</i>).</p>
161
<p>The item at position <i>end1</i> - 1 is assigned to that at position <i>end2</i> - 1; the item at position <i>end1</i> - 2 is assigned to that at position <i>end2</i> - 2; and so on.</p>
163
<pre> QList<QString> list;
164
list << "one" << "two" << "three";
166
QVector<QString> vect(5);
167
qCopyBackward(list.begin(), list.end(), vect.end());
168
// vect: [ "", "", "one", "two", "three" ]</pre>
169
<p>See also <a href="qtalgorithms.html#qCopy">qCopy</a>() and <a href="qtalgorithms.html#bidirectional-iterators">bidirectional iterators</a>.</p>
170
<h3 class="fn"><a name="qCount"></a>void qCount ( InputIterator <i>begin</i>, InputIterator <i>end</i>, const T & <i>value</i>, Size & <i>n</i> )</h3>
171
<p>Sets <i>n</i> to the number of occurrences of <i>value</i> in the range [<i>begin</i>, <i>end</i>).</p>
173
<pre> QList<int> list;
174
list << 3 << 3 << 6 << 6 << 6 << 8;
177
qCount(list.begin(), list.end(), 6, countOf6);
181
qCount(list.begin(), list.end(), 7, countOf7);
182
// countOf7 == 0</pre>
183
<p>This function requires the item type (in the example above, <tt>int</tt>) to implement <tt>operator==()</tt>.</p>
184
<p>See also <a href="qtalgorithms.html#input-iterators">input iterators</a>.</p>
185
<h3 class="fn"><a name="qDeleteAll"></a>void qDeleteAll ( ForwardIterator <i>begin</i>, ForwardIterator <i>end</i> )</h3>
186
<p>Deletes all the items in the range [<i>begin</i>, <i>end</i>) using the C++ <tt>delete</tt> operator. The item type must be a pointer type (for example, <tt>QWidget *</tt>).</p>
188
<pre> QList<Employee *> list;
189
list.append(new Employee("Blackpool", "Stephen"));
190
list.append(new Employee("Twist", "Oliver"));
192
qDeleteAll(list.begin(), list.end());
194
<p>Notice that qDeleteAll() doesn't remove the items from the container; it merely calls <tt>delete</tt> on them. In the example above, we call clear() on the container to remove the items.</p>
195
<p>See also <a href="qtalgorithms.html#forward-iterators">forward iterators</a>.</p>
196
<h3 class="fn"><a name="qDeleteAll-2"></a>void qDeleteAll ( const Container & <i>c</i> )</h3>
197
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
198
<p>This is the same as qDeleteAll(<i>c</i>.begin(), <i>c</i>.end()).</p>
199
<h3 class="fn"><a name="qEqual"></a>bool qEqual ( InputIterator1 <i>begin1</i>, InputIterator1 <i>end1</i>, InputIterator2 <i>begin2</i> )</h3>
200
<p>Compares the items in the range [<i>begin1</i>, <i>end1</i>) with the items in the range [<i>begin2</i>, ...). Returns true if all the items compare equal; otherwise returns false.</p>
202
<pre> QList<QString> list;
203
list << "one" << "two << "three";
205
QVector<QString> vect[3];
206
vect[0] = "one";
207
vect[1] = "two";
208
vect[2] = "three";
210
bool ret1 = qEqual(list.begin(), list.end(), vect.begin());
213
vect[2] = "seven";
214
bool rec2 = qEqual(list.begin(), list.end(), vect.begin());
215
// ret2 == false</pre>
216
<p>This function requires the item type (in the example above, <a href="qstring.html">QString</a>) to implement <tt>operator==()</tt>.</p>
217
<p>See also <a href="qtalgorithms.html#input-iterators">input iterators</a>.</p>
218
<h3 class="fn"><a name="qFill"></a>void qFill ( ForwardIterator <i>begin</i>, ForwardIterator <i>end</i>, const T & <i>value</i> )</h3>
219
<p>Fills the range [<i>begin</i>, <i>end</i>) with <i>value</i>.</p>
221
<pre> QList<QString> list;
222
list << "one" << "two" << "three";
224
qFill(list.begin(), list.end(), "eleven");
225
// list: [ "eleven", "eleven", "eleven" ]
227
qFill(list.begin() + 1, list.end(), "six");
228
// list: [ "eleven", "six", "six" ]</pre>
229
<p>See also <a href="qtalgorithms.html#qCopy">qCopy</a>() and <a href="qtalgorithms.html#forward-iterators">forward iterators</a>.</p>
230
<h3 class="fn"><a name="qFind"></a>InputIterator qFind ( InputIterator <i>begin</i>, InputIterator <i>end</i>, const T & <i>value</i> )</h3>
231
<p>Returns an iterator to the first occurrence of <i>value</i> in a container in the range [<i>begin</i>, <i>end</i>). Returns <i>end</i> if <i>value</i> isn't found.</p>
233
<pre> QList<QString> list;
234
list << "one" << "two" << "three";
236
QList<QString>::Iterator i1 = qFind(list.begin(), list.end(),
238
// i1 == list.begin() + 1
240
QList<QString>::Iterator i2 = qFind(list.begin(), list.end(),
241
"seventy");
242
// i2 == list.end()</pre>
243
<p>This function requires the item type (in the example above, <a href="qstring.html">QString</a>) to implement <tt>operator==()</tt>.</p>
244
<p>If the items in the range are in ascending order, you can get faster results by using <a href="qtalgorithms.html#qLowerBound">qLowerBound</a>() or <a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a>() instead of qFind().</p>
245
<p>See also <a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a>() and <a href="qtalgorithms.html#input-iterators">input iterators</a>.</p>
246
<h3 class="fn"><a name="qLowerBound"></a>RandomAccessIterator qLowerBound ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</h3>
247
<p>Performs a binary search of the range [<i>begin</i>, <i>end</i>) and returns the position of the first ocurrence of <i>value</i>. If no such item is found, returns the position where it should be inserted.</p>
248
<p>The items in the range [<i>begin</i>, <i>end</i>) must be sorted in ascending order; see <a href="qtalgorithms.html#qSort">qSort</a>().</p>
250
<pre> QList<int> list;
251
list << 3 << 3 << 6 << 6 << 6 << 8;
253
QList<int>::Iterator i = qLowerBound(list.begin(), list.end(), 5);
255
// list: [ 3, 3, 5, 6, 6, 6, 8 ]
257
i = qLowerBound(list.begin(), list.end(), 12);
259
// list: [ 3, 3, 5, 6, 6, 6, 8, 12 ]</pre>
260
<p>This function requires the item type (in the example above, <tt>int</tt>) to implement <tt>operator<()</tt>.</p>
261
<p>qLowerBound() can be used in conjunction with <a href="qtalgorithms.html#qUpperBound">qUpperBound</a>() to iterate over all occurrences of the same value:</p>
262
<pre> QVector<int> vect;
263
vect << 3 << 3 << 6 << 6 << 6 << 8;
264
QVector<int>::Iterator begin6 =
265
qLowerBound(vect.begin(), vect.end(), 6);
266
QVector<int>::Iterator end6 =
267
qUpperBound(begin6, vect.end(), 6);
269
QVector<int>::Iterator i = begin6;
274
// vect: [ 3, 3, 7, 7, 7, 8 ]</pre>
275
<p>See also <a href="qtalgorithms.html#qUpperBound">qUpperBound</a>() and <a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a>().</p>
276
<h3 class="fn"><a name="qSort"></a>void qSort ( BiIterator <i>begin</i>, BiIterator <i>end</i> )</h3>
277
<p>Sorts the items in range [<i>begin</i>, <i>end</i>) in ascending order using the heap sort algorithm.</p>
279
<pre> QList<int> list;
280
list << 33 << 12 << 68 << 6 << 12;
281
qSort(list.begin(), list.end());
282
// list: [ 6, 12, 12, 33, 68 ]</pre>
283
<p>The sort algorithm is efficient on large data sets. It operates in <a href="containers.html#linear-logarithmic-time">linear-logarithmic time</a>, O(<i>n</i> log <i>n</i>).</p>
284
<p>This function requires the item type (in the example above, <tt>int</tt>) to implement <tt>operator<()</tt>.</p>
285
<p>If of two items neither is less than the other, the items are taken to be equal. It is then undefined which one of the two items will appear before the other after the sort.</p>
286
<p>See also <a href="qtalgorithms.html#qStableSort">qStableSort</a>() and <a href="qtalgorithms.html#bidirectional-iterators">bidirectional iterators</a>.</p>
287
<h3 class="fn"><a name="qSort-2"></a>void qSort ( BiIterator <i>begin</i>, BiIterator <i>end</i>, LessThan <i>lessThan</i> )</h3>
288
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
289
<p>Uses the <i>lessThan</i> function instead of <tt>operator<()</tt> to compare the items.</p>
290
<p>For example, here's how to sort the strings in a <a href="qlist.html">QList</a><<a href="qstring.html">QString</a>> in case-insensitive alphabetical order:</p>
291
<pre> bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
293
return s1.toLower() < s2.toLower();
298
QList<QString> list;
299
list << "AlPha" << "beTA" << "gamma" << "DELTA";
300
qSort(list.begin(), list.end(), caseInsensitiveLessThan);
301
// list: [ "AlPha", "beTA", "DELTA", "gamma" ]
303
<p>To sort values in reverse order, pass <tt>qGreater<T>()</tt> as the <i>lessThan</i> parameter. For example:</p>
304
<pre> QList<int> list;
305
list << 33 << 12 << 68 << 6 << 12;
306
qSort(list.begin(), list.end(), qGreater<int>());
307
// list: [ 68, 33, 12, 12, 6 ]</pre>
308
<p>If of two items neither is "less than" the other, the items are taken to be equal. It is then undefined which one of the two items will appear before the other after the sort.</p>
309
<h3 class="fn"><a name="qSort-3"></a>void qSort ( Container & <i>container</i> )</h3>
310
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
311
<p>This is the same as qSort(<i>container</i>.begin(), <i>container</i>.end());</p>
312
<h3 class="fn"><a name="qStableSort"></a>void qStableSort ( BiIterator <i>begin</i>, BiIterator <i>end</i> )</h3>
313
<p>Sorts the items in range [<i>begin</i>, <i>end</i>) in ascending order using the heap sort algorithm.</p>
315
<pre> QList<int> list;
316
list << 33 << 12 << 68 << 6 << 12;
317
qStableSort(list.begin(), list.end());
318
// list: [ 6, 12, 12, 33, 68 ]</pre>
319
<p>The sort algorithm is efficient on large data sets. It operates in <a href="containers.html#linear-logarithmic-time">linear-logarithmic time</a>, O(<i>n</i> log <i>n</i>).</p>
320
<p>This function requires the item type (in the example above, <tt>int</tt>) to implement <tt>operator<()</tt>.</p>
321
<p>If of two items neither is less than the other, the items are taken to be equal. The item that appeared before the other in the original container will still appear first after the sort. This property is often useful when sorting user-visible data.</p>
322
<p>See also <a href="qtalgorithms.html#qSort">qSort</a>() and <a href="qtalgorithms.html#bidirectional-iterators">bidirectional iterators</a>.</p>
323
<h3 class="fn"><a name="qStableSort-2"></a>void qStableSort ( BiIterator <i>begin</i>, BiIterator <i>end</i>, LessThan <i>lessThan</i> )</h3>
324
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
325
<p>Uses the <i>lessThan</i> function instead of <tt>operator<()</tt> to compare the items.</p>
326
<p>For example, here's how to sort the strings in a <a href="qlist.html">QList</a><<a href="qstring.html">QString</a>> in case-insensitive alphabetical order:</p>
327
<pre> bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
329
return s1.toLower() < s2.toLower();
334
QList<QString> list;
335
list << "AlPha" << "beTA" << "gamma" << "DELTA";
336
qStableSort(list.begin(), list.end(), caseInsensitiveLessThan);
337
// list: [ "AlPha", "beTA", "DELTA", "gamma" ]
339
<p>To sort values in reverse order, pass <tt>qGreater<T>()</tt> as the <i>lessThan</i> parameter. For example:</p>
340
<pre> QList<int> list;
341
list << 33 << 12 << 68 << 6 << 12;
342
qStableSort(list.begin(), list.end(), qGreater<int>());
343
// list: [ 68, 33, 12, 12, 6 ]</pre>
344
<p>If of two items neither is "less than" the other, the items are taken to be equal. The item that appeared before the other in the original container will still appear first after the sort. This property is often useful when sorting user-visible data.</p>
345
<h3 class="fn"><a name="qStableSort-3"></a>void qStableSort ( Container & <i>container</i> )</h3>
346
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
347
<p>This is the same as qStableSort(<i>container</i>.begin(), <i>container</i>.end());</p>
348
<h3 class="fn"><a name="qSwap"></a>void qSwap ( T & <i>var1</i>, T & <i>var2</i> )</h3>
349
<p>Exchanges the values of variables <i>var1</i> and <i>var2</i>.</p>
351
<pre> double pi = 3.14;
355
// pi == 2.71, e == 3.14</pre>
356
<h3 class="fn"><a name="qUpperBound"></a>RandomAccessIterator qUpperBound ( RandomAccessIterator <i>begin</i>, RandomAccessIterator <i>end</i>, const T & <i>value</i> )</h3>
357
<p>Performs a binary search of the range [<i>begin</i>, <i>end</i>) and returns the position of the one-past-the-last occurrence of <i>value</i>. If no such item is found, returns the position where the item should be inserted.</p>
358
<p>The items in the range [<i>begin</i>, <i>end</i>) must be sorted in ascending order; see <a href="qtalgorithms.html#qSort">qSort</a>().</p>
360
<pre> QList<int> list;
361
list << 3 << 3 << 6 << 6 << 6 << 8;
363
QList<int>::Iterator i = qUpperBound(list.begin(), list.end(), 5);
365
// list: [ 3, 3, 5, 6, 6, 6, 8 ]
367
i = qUpperBound(list.begin(), list.end(), 12);
369
// list: [ 3, 3, 5, 6, 6, 6, 8, 12 ]</pre>
370
<p>This function requires the item type (in the example above, <tt>int</tt>) to implement <tt>operator<()</tt>.</p>
371
<p>qUpperBound() can be used in conjunction with <a href="qtalgorithms.html#qLowerBound">qLowerBound</a>() to iterate over all occurrences of the same value:</p>
372
<pre> QVector<int> vect;
373
vect << 3 << 3 << 6 << 6 << 6 << 8;
374
QVector<int>::Iterator begin6 =
375
qLowerBound(vect.begin(), vect.end(), 6);
376
QVector<int>::Iterator end6 =
377
qUpperBound(vect.begin(), vect.end(), 6);
379
QVector<int>::Iterator i = begin6;
384
// vect: [ 3, 3, 7, 7, 7, 8 ]</pre>
385
<p>See also <a href="qtalgorithms.html#qLowerBound">qLowerBound</a>() and <a href="qtalgorithms.html#qBinaryFind">qBinaryFind</a>().</p>
386
<p /><address><hr /><div align="center">
387
<table width="100%" cellspacing="0" border="0"><tr class="address">
388
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
389
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
390
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
391
</tr></table></div></address></body>