~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qtalgorithms.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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 -->
 
6
<head>
 
7
    <title>Qt 4.0: &lt;QtAlgorithms&gt; - 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>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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">&lt;QtAlgorithms&gt; - Generic Algorithms</h1>
 
21
<p>The &lt;QtAlgorithms&gt; header file provides generic template-based algorithms. <a href="#details">More...</a></p>
 
22
<a name="Functionsx"></a>
 
23
<h3>Functions</h3>
 
24
<ul>
 
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 &amp; <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 &amp; <i>value</i>, Size &amp; <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 &amp; <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 &amp; <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 &amp; <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 &amp; <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 &amp; <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 &amp; <i>container</i> )</li>
 
41
<li><div class="fn"/>void <b><a href="qtalgorithms.html#qSwap">qSwap</a></b> ( T &amp; <i>var1</i>, T &amp; <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 &amp; <i>value</i> )</li>
 
43
</ul>
 
44
<a name="details"></a>
 
45
<hr />
 
46
<h2>Detailed Description</h2>
 
47
<p>The &lt;QtAlgorithms&gt; header file provides generic template-based algorithms.</p>
 
48
<p>Qt provides a number of global template functions in <tt>&lt;QtAlgorithms&gt;</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>&lt;algorithm&gt;</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>&nbsp;   QList&lt;QString&gt; list;
 
54
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two&quot; &lt;&lt; &quot;three&quot;;
 
55
 
 
56
    qFill(list.begin(), list.end(), &quot;eleven&quot;);
 
57
    // list: [ &quot;eleven&quot;, &quot;eleven&quot;, &quot;eleven&quot; ]</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>&nbsp;   qFill(list.begin() + 1, list.end(), &quot;six&quot;);
 
60
    // list: [ &quot;eleven&quot;, &quot;six&quot;, &quot;six&quot; ]</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;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>&nbsp;   QChar resolveEntity(const QString &amp;entity)
 
65
    {
 
66
        static const QLatin1String name_table[] = {
 
67
            &quot;AElig&quot;, &quot;Aacute&quot;, ..., &quot;zwnj&quot;
 
68
        };
 
69
        static const ushort value_table[] = {
 
70
            0x0061, 0x00c1, ..., 0x200c
 
71
        };
 
72
        int N = sizeof(name_table) / sizeof(name_table[0]);
 
73
 
 
74
        const QLatin1String *name = qBinaryFind(name_table, name_table + N,
 
75
                                                entity);
 
76
        int index = name - name_table;
 
77
        if (index == N)
 
78
            return QChar();
 
79
 
 
80
        return QChar(value_table[index]);
 
81
    }</pre>
 
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>&nbsp;   QChar resolveEntity(const QString &amp;entity)
 
84
    {
 
85
        static QMap&lt;QString, int&gt; entityMap;
 
86
 
 
87
        if (!entityMap) {
 
88
            entityMap.insert(&quot;AElig&quot;, 0x0061);
 
89
            entityMap.insert(&quot;Aacute&quot;, 0x00c1);
 
90
            ...
 
91
            entityMap.insert(&quot;zwnj&quot;, 0x200c);
 
92
        }
 
93
        return QChar(entityMap.value(entity));
 
94
    }</pre>
 
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 &lt; j</tt></td><td>returns true if iterator <tt>j</tt> comes after iterator <tt>i</tt></td></tr>
 
125
</table>
 
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">&lt;QtGlobal&gt;</a>.</p>
 
128
<hr />
 
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 &amp; <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>
 
134
<p>Example:</p>
 
135
<pre>&nbsp;   QVector&lt;int&gt; vect;
 
136
    vect &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
137
 
 
138
    QVector&lt;int&gt;::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&lt;()</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>
 
147
<p>Example:</p>
 
148
<pre>&nbsp;   QList&lt;QString&gt; list;
 
149
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two&quot; &lt;&lt; &quot;three&quot;;
 
150
 
 
151
    QVector&lt;QString&gt; vect1(3);
 
152
    qCopy(list.begin(), list.end(), vect1.begin());
 
153
    // vect: [ &quot;one&quot;, &quot;two&quot;, &quot;three&quot; ]
 
154
 
 
155
    QVector&lt;QString&gt; vect2(8);
 
156
    qCopy(list.begin(), list.end(), vect2.begin() + 2);
 
157
    // vect: [ &quot;&quot;, &quot;&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot; ]</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>
 
162
<p>Example:</p>
 
163
<pre>&nbsp;   QList&lt;QString&gt; list;
 
164
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two&quot; &lt;&lt; &quot;three&quot;;
 
165
 
 
166
    QVector&lt;QString&gt; vect(5);
 
167
    qCopyBackward(list.begin(), list.end(), vect.end());
 
168
    // vect: [ &quot;&quot;, &quot;&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot; ]</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 &amp; <i>value</i>, Size &amp; <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>
 
172
<p>Example:</p>
 
173
<pre>&nbsp;   QList&lt;int&gt; list;
 
174
    list &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
175
 
 
176
    int countOf6;
 
177
    qCount(list.begin(), list.end(), 6, countOf6);
 
178
    // countOf6 == 3
 
179
 
 
180
    int countOf7;
 
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>
 
187
<p>Example:</p>
 
188
<pre>&nbsp;   QList&lt;Employee *&gt; list;
 
189
    list.append(new Employee(&quot;Blackpool&quot;, &quot;Stephen&quot;));
 
190
    list.append(new Employee(&quot;Twist&quot;, &quot;Oliver&quot;));
 
191
 
 
192
    qDeleteAll(list.begin(), list.end());
 
193
    list.clear();</pre>
 
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 &amp; <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>
 
201
<p>Example:</p>
 
202
<pre>&nbsp;   QList&lt;QString&gt; list;
 
203
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two &lt;&lt; &quot;three&quot;;
 
204
 
 
205
    QVector&lt;QString&gt; vect[3];
 
206
    vect[0] = &quot;one&quot;;
 
207
    vect[1] = &quot;two&quot;;
 
208
    vect[2] = &quot;three&quot;;
 
209
 
 
210
    bool ret1 = qEqual(list.begin(), list.end(), vect.begin());
 
211
    // ret1 == true
 
212
 
 
213
    vect[2] = &quot;seven&quot;;
 
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 &amp; <i>value</i> )</h3>
 
219
<p>Fills the range [<i>begin</i>, <i>end</i>) with <i>value</i>.</p>
 
220
<p>Example:</p>
 
221
<pre>&nbsp;   QList&lt;QString&gt; list;
 
222
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two&quot; &lt;&lt; &quot;three&quot;;
 
223
 
 
224
    qFill(list.begin(), list.end(), &quot;eleven&quot;);
 
225
    // list: [ &quot;eleven&quot;, &quot;eleven&quot;, &quot;eleven&quot; ]
 
226
 
 
227
    qFill(list.begin() + 1, list.end(), &quot;six&quot;);
 
228
    // list: [ &quot;eleven&quot;, &quot;six&quot;, &quot;six&quot; ]</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 &amp; <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>
 
232
<p>Example:</p>
 
233
<pre>&nbsp;   QList&lt;QString&gt; list;
 
234
    list &lt;&lt; &quot;one&quot; &lt;&lt; &quot;two&quot; &lt;&lt; &quot;three&quot;;
 
235
 
 
236
    QList&lt;QString&gt;::Iterator i1 = qFind(list.begin(), list.end(),
 
237
                                        &quot;two&quot;);
 
238
    // i1 == list.begin() + 1
 
239
 
 
240
    QList&lt;QString&gt;::Iterator i2 = qFind(list.begin(), list.end(),
 
241
                                        &quot;seventy&quot;);
 
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 &amp; <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>
 
249
<p>Example:</p>
 
250
<pre>&nbsp;   QList&lt;int&gt; list;
 
251
    list &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
252
 
 
253
    QList&lt;int&gt;::Iterator i = qLowerBound(list.begin(), list.end(), 5);
 
254
    list.insert(i, 5);
 
255
    // list: [ 3, 3, 5, 6, 6, 6, 8 ]
 
256
 
 
257
    i = qLowerBound(list.begin(), list.end(), 12);
 
258
    list.insert(i, 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&lt;()</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>&nbsp;   QVector&lt;int&gt; vect;
 
263
    vect &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
264
    QVector&lt;int&gt;::Iterator begin6 =
 
265
            qLowerBound(vect.begin(), vect.end(), 6);
 
266
    QVector&lt;int&gt;::Iterator end6 =
 
267
            qUpperBound(begin6, vect.end(), 6);
 
268
 
 
269
    QVector&lt;int&gt;::Iterator i = begin6;
 
270
    while (i != end6) {
 
271
        *i = 7;
 
272
        ++i;
 
273
    }
 
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>
 
278
<p>Example:</p>
 
279
<pre>&nbsp;   QList&lt;int&gt; list;
 
280
    list &lt;&lt; 33 &lt;&lt; 12 &lt;&lt; 68 &lt;&lt; 6 &lt;&lt; 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&lt;()</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&lt;()</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>&lt;<a href="qstring.html">QString</a>&gt; in case-insensitive alphabetical order:</p>
 
291
<pre>&nbsp;   bool caseInsensitiveLessThan(const QString &amp;s1, const QString &amp;s2)
 
292
    {
 
293
        return s1.toLower() &lt; s2.toLower();
 
294
    }
 
295
 
 
296
    int doSomething()
 
297
    {
 
298
        QList&lt;QString&gt; list;
 
299
        list &lt;&lt; &quot;AlPha&quot; &lt;&lt; &quot;beTA&quot; &lt;&lt; &quot;gamma&quot; &lt;&lt; &quot;DELTA&quot;;
 
300
        qSort(list.begin(), list.end(), caseInsensitiveLessThan);
 
301
        // list: [ &quot;AlPha&quot;, &quot;beTA&quot;, &quot;DELTA&quot;, &quot;gamma&quot; ]
 
302
    }</pre>
 
303
<p>To sort values in reverse order, pass <tt>qGreater&lt;T&gt;()</tt> as the <i>lessThan</i> parameter. For example:</p>
 
304
<pre>&nbsp;   QList&lt;int&gt; list;
 
305
    list &lt;&lt; 33 &lt;&lt; 12 &lt;&lt; 68 &lt;&lt; 6 &lt;&lt; 12;
 
306
    qSort(list.begin(), list.end(), qGreater&lt;int&gt;());
 
307
    // list: [ 68, 33, 12, 12, 6 ]</pre>
 
308
<p>If of two items neither is &quot;less than&quot; 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 &amp; <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>
 
314
<p>Example:</p>
 
315
<pre>&nbsp;   QList&lt;int&gt; list;
 
316
    list &lt;&lt; 33 &lt;&lt; 12 &lt;&lt; 68 &lt;&lt; 6 &lt;&lt; 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&lt;()</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&lt;()</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>&lt;<a href="qstring.html">QString</a>&gt; in case-insensitive alphabetical order:</p>
 
327
<pre>&nbsp;   bool caseInsensitiveLessThan(const QString &amp;s1, const QString &amp;s2)
 
328
    {
 
329
        return s1.toLower() &lt; s2.toLower();
 
330
    }
 
331
 
 
332
    int doSomething()
 
333
    {
 
334
        QList&lt;QString&gt; list;
 
335
        list &lt;&lt; &quot;AlPha&quot; &lt;&lt; &quot;beTA&quot; &lt;&lt; &quot;gamma&quot; &lt;&lt; &quot;DELTA&quot;;
 
336
        qStableSort(list.begin(), list.end(), caseInsensitiveLessThan);
 
337
        // list: [ &quot;AlPha&quot;, &quot;beTA&quot;, &quot;DELTA&quot;, &quot;gamma&quot; ]
 
338
    }</pre>
 
339
<p>To sort values in reverse order, pass <tt>qGreater&lt;T&gt;()</tt> as the <i>lessThan</i> parameter. For example:</p>
 
340
<pre>&nbsp;   QList&lt;int&gt; list;
 
341
    list &lt;&lt; 33 &lt;&lt; 12 &lt;&lt; 68 &lt;&lt; 6 &lt;&lt; 12;
 
342
    qStableSort(list.begin(), list.end(), qGreater&lt;int&gt;());
 
343
    // list: [ 68, 33, 12, 12, 6 ]</pre>
 
344
<p>If of two items neither is &quot;less than&quot; 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 &amp; <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 &amp; <i>var1</i>, T &amp; <i>var2</i> )</h3>
 
349
<p>Exchanges the values of variables <i>var1</i> and <i>var2</i>.</p>
 
350
<p>Example:</p>
 
351
<pre>&nbsp;   double pi = 3.14;
 
352
    double e = 2.71;
 
353
 
 
354
    qSwap(pi, e);
 
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 &amp; <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>
 
359
<p>Example:</p>
 
360
<pre>&nbsp;   QList&lt;int&gt; list;
 
361
    list &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
362
 
 
363
    QList&lt;int&gt;::Iterator i = qUpperBound(list.begin(), list.end(), 5);
 
364
    list.insert(i, 5);
 
365
    // list: [ 3, 3, 5, 6, 6, 6, 8 ]
 
366
 
 
367
    i = qUpperBound(list.begin(), list.end(), 12);
 
368
    list.insert(i, 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&lt;()</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>&nbsp;   QVector&lt;int&gt; vect;
 
373
    vect &lt;&lt; 3 &lt;&lt; 3 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 6 &lt;&lt; 8;
 
374
    QVector&lt;int&gt;::Iterator begin6 =
 
375
            qLowerBound(vect.begin(), vect.end(), 6);
 
376
    QVector&lt;int&gt;::Iterator end6 =
 
377
            qUpperBound(vect.begin(), vect.end(), 6);
 
378
 
 
379
    QVector&lt;int&gt;::Iterator i = begin6;
 
380
    while (i != end6) {
 
381
        *i = 7;
 
382
        ++i;
 
383
    }
 
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 &copy; 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>
 
392
</html>