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

« back to all changes in this revision

Viewing changes to doc/html/qlinkedlist-iterator.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/src/corelib/tools/qlinkedlist.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QLinkedList::iterator Class Reference</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">QLinkedList::iterator Class Reference</h1>
 
21
<p>The QLinkedList::iterator class provides an STL-style non-const iterator for <a href="qlinkedlist.html">QLinkedList</a>. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QLinkedList&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p>Inherited by <a href="q3valuelistiterator.html">Q3ValueListIterator</a>.</p>
 
24
<ul>
 
25
<li><a href="qlinkedlist-iterator-members.html">List of all members, including inherited members</a></li>
 
26
</ul>
 
27
<a name="public-functions"></a>
 
28
<h3>Public Functions</h3>
 
29
<ul>
 
30
<li><div class="fn"/><b><a href="qlinkedlist-iterator.html#iterator">iterator</a></b> ()</li>
 
31
<li><div class="fn"/><b><a href="qlinkedlist-iterator.html#iterator-3">iterator</a></b> ( const iterator &amp; <i>other</i> )</li>
 
32
<li><div class="fn"/>bool <b><a href="qlinkedlist-iterator.html#operator-not-eq">operator!=</a></b> ( const iterator &amp; <i>other</i> ) const</li>
 
33
<li><div class="fn"/>bool <b><a href="qlinkedlist-iterator.html#operator-not-eq-2">operator!=</a></b> ( const const_iterator &amp; <i>other</i> ) const</li>
 
34
<li><div class="fn"/>T &amp; <b><a href="qlinkedlist-iterator.html#operator-2a">operator*</a></b> () const</li>
 
35
<li><div class="fn"/>iterator <b><a href="qlinkedlist-iterator.html#operator-2b">operator+</a></b> ( int <i>j</i> ) const</li>
 
36
<li><div class="fn"/>iterator &amp; <b><a href="qlinkedlist-iterator.html#operator-2b-2b">operator++</a></b> ()</li>
 
37
<li><div class="fn"/>iterator <b><a href="qlinkedlist-iterator.html#operator-2b-2b-2">operator++</a></b> ( int )</li>
 
38
<li><div class="fn"/>iterator &amp; <b><a href="qlinkedlist-iterator.html#operator-2b-eq">operator+=</a></b> ( int <i>j</i> )</li>
 
39
<li><div class="fn"/>iterator <b><a href="qlinkedlist-iterator.html#operator-">operator-</a></b> ( int <i>j</i> ) const</li>
 
40
<li><div class="fn"/>iterator &amp; <b><a href="qlinkedlist-iterator.html#operator--">operator--</a></b> ()</li>
 
41
<li><div class="fn"/>iterator <b><a href="qlinkedlist-iterator.html#operator---2">operator--</a></b> ( int )</li>
 
42
<li><div class="fn"/>iterator &amp; <b><a href="qlinkedlist-iterator.html#operator--eq">operator-=</a></b> ( int <i>j</i> )</li>
 
43
<li><div class="fn"/>T * <b><a href="qlinkedlist-iterator.html#operator--gt">operator-&gt;</a></b> () const</li>
 
44
<li><div class="fn"/>iterator &amp; <b><a href="qlinkedlist-iterator.html#operator-eq">operator=</a></b> ( const iterator &amp; <i>other</i> )</li>
 
45
<li><div class="fn"/>bool <b><a href="qlinkedlist-iterator.html#operator-eq-eq">operator==</a></b> ( const iterator &amp; <i>other</i> ) const</li>
 
46
<li><div class="fn"/>bool <b><a href="qlinkedlist-iterator.html#operator-eq-eq-2">operator==</a></b> ( const const_iterator &amp; <i>other</i> ) const</li>
 
47
</ul>
 
48
<a name="details"></a>
 
49
<hr />
 
50
<h2>Detailed Description</h2>
 
51
<p>The QLinkedList::iterator class provides an STL-style non-const iterator for <a href="qlinkedlist.html">QLinkedList</a>.</p>
 
52
<p><a href="qlinkedlist.html">QLinkedList</a> features both <a href="containers.html#stl-style-iterators">STL-style iterators</a> and <a href="containers.html#java-style-iterators">Java-style iterators</a>. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.</p>
 
53
<p><a href="qlinkedlist.html">QLinkedList</a>&lt;T&gt;::iterator allows you to iterate over a <a href="qlinkedlist.html">QLinkedList</a>&lt;T&gt; and to modify the list item associated with the iterator. If you want to iterate over a const <a href="qlinkedlist.html">QLinkedList</a>, use <a href="qlinkedlist-const-iterator.html">QLinkedList::const_iterator</a> instead. It is generally good practice to use <a href="qlinkedlist-const-iterator.html">QLinkedList::const_iterator</a> on a non-const <a href="qlinkedlist.html">QLinkedList</a> as well, unless you need to change the <a href="qlinkedlist.html">QLinkedList</a> through the iterator. Const iterators are slightly faster, and can improve code readability.</p>
 
54
<p>The default QLinkedList::iterator constructor creates an uninitialized iterator. You must initialize it using a function like <a href="qlinkedlist.html#begin">QLinkedList::begin</a>(), <a href="qlinkedlist.html#end">QLinkedList::end</a>(), or <a href="qlinkedlist.html#insert">QLinkedList::insert</a>() before you can start iterating. Here's a typical loop that prints all the items stored in a list:</p>
 
55
<pre>&nbsp;   QLinkedList&lt;QString&gt; list;
 
56
    list.append(&quot;January&quot;);
 
57
    list.append(&quot;February&quot;);
 
58
    ...
 
59
    list.append(&quot;December&quot;);
 
60
 
 
61
    QLinkedList&lt;QString&gt;::iterator i;
 
62
    for (i = list.begin(); i != list.end(); ++i)
 
63
        cout &lt;&lt; *i &lt;&lt; endl;</pre>
 
64
<p>STL-style iterators can be used as arguments to <a href="qtalgorithms.html">generic algorithms</a>. For example, here's how to find an item in the list using the <a href="qtalgorithms.html#qFind">qFind</a>() algorithm:</p>
 
65
<pre>&nbsp;   QLinkedList&lt;QString&gt; list;
 
66
    ...
 
67
    QLinkedList&lt;QString&gt;::iterator it = qFind(list.begin(),
 
68
                                              list.end(), &quot;Joel&quot;);
 
69
    if (it != list.end())
 
70
        cout &lt;&lt; &quot;Found Joel&quot; &lt;&lt; endl;</pre>
 
71
<p>Let's see a few examples of things we can do with a QLinkedList::iterator that we cannot do with a <a href="qlinkedlist-const-iterator.html">QLinkedList::const_iterator</a>. Here's an example that increments every value stored in a <a href="qlinkedlist.html">QLinkedList</a>&lt;int&gt; by 2:</p>
 
72
<pre>&nbsp;   QLinkedList&lt;int&gt;::iterator i;
 
73
    for (i = list.begin(); i != list.end(); ++i)
 
74
        *i += 2;</pre>
 
75
<p>Here's an example that removes all the items that start with an underscore character in a <a href="qlinkedlist.html">QLinkedList</a>&lt;<a href="qstring.html">QString</a>&gt;:</p>
 
76
<pre>&nbsp;   QLinkedList&lt;QString&gt; list;
 
77
    ...
 
78
    QLinkedList&lt;QString&gt;::iterator i = list.begin();
 
79
    while (i != list.end()) {
 
80
        if ((*i).startsWith(&quot;_&quot;))
 
81
            i = list.erase(i);
 
82
        else
 
83
            ++i;
 
84
    }</pre>
 
85
<p>The call to <a href="qlinkedlist.html#erase">QLinkedList::erase</a>() removes the item pointed to by the iterator from the list, and returns an iterator to the next item. Here's another way of removing an item while iterating:</p>
 
86
<pre>&nbsp;   QLinkedList&lt;QString&gt;::iterator i = list.begin();
 
87
    while (i != list.end()) {
 
88
        QLinkedList&lt;QString&gt;::iterator previous = i;
 
89
        ++i;
 
90
        if ((*previous).startsWith(&quot;_&quot;))
 
91
            list.erase(previous);
 
92
    }</pre>
 
93
<p>It might be tempting to write code like this:</p>
 
94
<pre>&nbsp;   // WRONG
 
95
    while (i != list.end()) {
 
96
        if ((*i).startsWith(&quot;_&quot;))
 
97
            list.erase(i);
 
98
        ++i;
 
99
    }</pre>
 
100
<p>However, this will potentially crash in <tt>++i</tt>, because <tt>i</tt> is a dangling iterator after the call to <a href="qlinkedlist.html#erase">erase</a>().</p>
 
101
<p>Multiple iterators can be used on the same list. If you add items to the list, existing iterators will remain valid. If you remove items from the list, iterators that point to the removed items will become dangling iterators.</p>
 
102
<p>See also <a href="qlinkedlist-const-iterator.html">QLinkedList::const_iterator</a> and <a href="qmutablelinkedlistiterator.html">QMutableLinkedListIterator</a>.</p>
 
103
<hr />
 
104
<h2>Member Function Documentation</h2>
 
105
<h3 class="fn"><a name="iterator"></a>iterator::iterator ()</h3>
 
106
<p>Constructs an uninitialized iterator.</p>
 
107
<p>Functions like operator*() and operator++() should not be called on an uninitialized iterartor. Use operator=() to assign a value to it before using it.</p>
 
108
<p>See also <a href="qlinkedlist.html#begin">QLinkedList::begin</a>() and <a href="qlinkedlist.html#end">QLinkedList::end</a>().</p>
 
109
<h3 class="fn"><a name="iterator-3"></a>iterator::iterator ( const iterator &amp; <i>other</i> )</h3>
 
110
<p>Constructs a copy of <i>other</i>.</p>
 
111
<h3 class="fn"><a name="operator-not-eq"></a>bool iterator::operator!= ( const iterator &amp; <i>other</i> ) const</h3>
 
112
<p>Returns true if <i>other</i> points to a different item than this iterator; otherwise returns false.</p>
 
113
<p>See also <a href="qlinkedlist-iterator.html#operator-eq-eq">operator==</a>().</p>
 
114
<h3 class="fn"><a name="operator-not-eq-2"></a>bool iterator::operator!= ( const <a href="qlinkedlist-const-iterator.html">const_iterator</a> &amp; <i>other</i> ) const</h3>
 
115
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
116
<p>Returns true if <i>other</i> points to a different item than this iterator; otherwise returns false.</p>
 
117
<p>See also <a href="qlinkedlist-iterator.html#operator-eq-eq">operator==</a>().</p>
 
118
<h3 class="fn"><a name="operator-2a"></a>T &amp; iterator::operator* () const</h3>
 
119
<p>Returns a modifiable reference to the current item.</p>
 
120
<p>You can change the value of an item by using operator*() on the left side of an assignment, for example:</p>
 
121
<pre>&nbsp;   if (*it == &quot;Hello&quot;)
 
122
        *it = &quot;Bonjour&quot;;</pre>
 
123
<p>See also <a href="qlinkedlist-iterator.html#operator--gt">operator-&gt;</a>().</p>
 
124
<h3 class="fn"><a name="operator-2b"></a>iterator iterator::operator+ ( int <i>j</i> ) const</h3>
 
125
<p>Returns an iterator to the item at <i>j</i> positions forward from this iterator. (If <i>j</i> is negative, the iterator goes backward.)</p>
 
126
<p>This operation can be slow for large <i>j</i> values.</p>
 
127
<p>See also <a href="qlinkedlist-iterator.html#operator-">operator-</a>().</p>
 
128
<h3 class="fn"><a name="operator-2b-2b"></a>iterator &amp; iterator::operator++ ()</h3>
 
129
<p>The prefix ++ operator (<tt>++it</tt>) advances the iterator to the next item in the list and returns an iterator to the new current item.</p>
 
130
<p>Calling this function on <a href="qlinkedlist.html#end">QLinkedList::end</a>() leads to undefined results.</p>
 
131
<p>See also <a href="qlinkedlist-iterator.html#operator--">operator--</a>().</p>
 
132
<h3 class="fn"><a name="operator-2b-2b-2"></a>iterator iterator::operator++ ( int )</h3>
 
133
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
134
<p>The postfix ++ operator (<tt>it++</tt>) advances the iterator to the next item in the list and returns an iterator to the previously current item.</p>
 
135
<h3 class="fn"><a name="operator-2b-eq"></a>iterator &amp; iterator::operator+= ( int <i>j</i> )</h3>
 
136
<p>Advances the iterator by <i>j</i> items. (If <i>j</i> is negative, the iterator goes backward.)</p>
 
137
<p>See also <a href="qlinkedlist-iterator.html#operator--eq">operator-=</a>() and <a href="qlinkedlist-iterator.html#operator-2b">operator+</a>().</p>
 
138
<h3 class="fn"><a name="operator-"></a>iterator iterator::operator- ( int <i>j</i> ) const</h3>
 
139
<p>Returns an iterator to the item at <i>j</i> positions backward from this iterator. (If <i>j</i> is negative, the iterator goes forward.)</p>
 
140
<p>This operation can be slow for large <i>j</i> values.</p>
 
141
<p>See also <a href="qlinkedlist-iterator.html#operator-2b">operator+</a>().</p>
 
142
<h3 class="fn"><a name="operator--"></a>iterator &amp; iterator::operator-- ()</h3>
 
143
<p>The prefix -- operator (<tt>--it</tt>) makes the preceding item current and returns an iterator to the new current item.</p>
 
144
<p>Calling this function on <a href="qlinkedlist.html#begin">QLinkedList::begin</a>() leads to undefined results.</p>
 
145
<p>See also <a href="qlinkedlist-iterator.html#operator-2b-2b">operator++</a>().</p>
 
146
<h3 class="fn"><a name="operator---2"></a>iterator iterator::operator-- ( int )</h3>
 
147
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
148
<p>The postfix -- operator (<tt>it--</tt>) makes the preceding item current and returns an iterator to the previously current item.</p>
 
149
<h3 class="fn"><a name="operator--eq"></a>iterator &amp; iterator::operator-= ( int <i>j</i> )</h3>
 
150
<p>Makes the iterator go back by <i>j</i> items. (If <i>j</i> is negative, the iterator goes forward.)</p>
 
151
<p>See also <a href="qlinkedlist-iterator.html#operator-2b-eq">operator+=</a>() and <a href="qlinkedlist-iterator.html#operator-">operator-</a>().</p>
 
152
<h3 class="fn"><a name="operator--gt"></a>T * iterator::operator-&gt; () const</h3>
 
153
<p>Returns a pointer to the current item.</p>
 
154
<p>See also <a href="qlinkedlist-iterator.html#operator-2a">operator*</a>().</p>
 
155
<h3 class="fn"><a name="operator-eq"></a>iterator &amp; iterator::operator= ( const iterator &amp; <i>other</i> )</h3>
 
156
<p>Assigns <i>other</i> to this iterator.</p>
 
157
<h3 class="fn"><a name="operator-eq-eq"></a>bool iterator::operator== ( const iterator &amp; <i>other</i> ) const</h3>
 
158
<p>Returns true if <i>other</i> points to the same item as this iterator; otherwise returns false.</p>
 
159
<p>See also <a href="qlinkedlist-iterator.html#operator-not-eq">operator!=</a>().</p>
 
160
<h3 class="fn"><a name="operator-eq-eq-2"></a>bool iterator::operator== ( const <a href="qlinkedlist-const-iterator.html">const_iterator</a> &amp; <i>other</i> ) const</h3>
 
161
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
162
<p>Returns true if <i>other</i> points to the same item as this iterator; otherwise returns false.</p>
 
163
<p>See also <a href="qlinkedlist-iterator.html#operator-not-eq">operator!=</a>().</p>
 
164
<p /><address><hr /><div align="center">
 
165
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
166
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
167
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
168
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
169
</tr></table></div></address></body>
 
170
</html>