• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Element iteration</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Icl">
8<link rel="up" href="../function_reference.html" title="Function Reference">
9<link rel="prev" href="iterator_related.html" title="Iterator related">
10<link rel="next" href="streaming__conversion.html" title="Streaming, conversion">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="iterator_related.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_reference.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="streaming__conversion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_icl.function_reference.element_iteration"></a><a class="link" href="element_iteration.html" title="Element iteration">Element
28      iteration</a>
29</h3></div></div></div>
30<p>
31        This section refers to <span class="emphasis"><em><span class="bold"><strong>element iteration</strong></span></em></span>
32        over <span class="emphasis"><em><span class="bold"><strong>interval containers</strong></span></em></span>.
33        Element iterators are available as associated types on interval sets and
34        interval maps.
35      </p>
36<div class="informaltable"><table class="table">
37<colgroup>
38<col>
39<col>
40</colgroup>
41<thead><tr>
42<th>
43                <p>
44                  Variant
45                </p>
46              </th>
47<th>
48                <p>
49                  Associated element iterator type for interval container <code class="computeroutput"><span class="identifier">T</span></code>
50                </p>
51              </th>
52</tr></thead>
53<tbody>
54<tr>
55<td>
56                <p>
57                  forward
58                </p>
59              </td>
60<td>
61                <p>
62                  <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">element_iterator</span></code>
63                </p>
64              </td>
65</tr>
66<tr>
67<td>
68                <p>
69                  const forward
70                </p>
71              </td>
72<td>
73                <p>
74                  <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">element_const_iterator</span></code>
75                </p>
76              </td>
77</tr>
78<tr>
79<td>
80                <p>
81                  reverse
82                </p>
83              </td>
84<td>
85                <p>
86                  <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">element_reverse_iterator</span></code>
87                </p>
88              </td>
89</tr>
90<tr>
91<td>
92                <p>
93                  const reverse
94                </p>
95              </td>
96<td>
97                <p>
98                  <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">element_const_reverse_iterator</span></code>
99                </p>
100              </td>
101</tr>
102</tbody>
103</table></div>
104<p>
105        There are also associated iterators types <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">iterator</span></code>,
106        <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">const_iterator</span></code>, <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">reverse_iterator</span></code>
107        and <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">reverse_const_iterator</span></code> on interval containers.
108        These are <span class="emphasis"><em><span class="bold"><strong>segment iterators</strong></span></em></span>.
109        Segment iterators are "first citizen iterators". Iteration over
110        segments is fast, compared to an iteration over elements, particularly if
111        intervals are large. But if we want to view our interval containers as containers
112        of elements that are usable with std::algoritms, we need to iterate over
113        elements.
114      </p>
115<p>
116        Iteration over elements . . .
117      </p>
118<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
119<li class="listitem">
120            is possible only for integral or discrete <code class="computeroutput"><span class="identifier">domain_types</span></code>
121          </li>
122<li class="listitem">
123            can be very <span class="emphasis"><em><span class="bold"><strong>slow</strong></span></em></span>
124            if the intervals are very large.
125          </li>
126<li class="listitem">
127            and is therefore <span class="emphasis"><em><span class="bold"><strong>depreciated</strong></span></em></span>
128          </li>
129</ul></div>
130<p>
131        On the other hand, sometimes iteration over interval containers on the element
132        level might be desired, if you have some interface that works for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">SortedAssociativeContainers</span></code>
133        of elements and you need to quickly use it with an interval container. Accepting
134        the poorer performance might be less bothersome at times than adjusting your
135        whole interface for segment iteration.
136      </p>
137<div class="caution"><table border="0" summary="Caution">
138<tr>
139<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
140<th align="left">Caution</th>
141</tr>
142<tr><td align="left" valign="top"><p>
143          So we advice you to choose element iteration over interval containers
144          <span class="emphasis"><em><span class="bold"><strong>judiciously</strong></span></em></span>. Do not
145          use element iteration <span class="emphasis"><em><span class="bold"><strong>by default or habitual</strong></span></em></span>.
146          Always try to achieve results using member functions, global functions
147          or operators (preferably inplace versions) or iteration over segments first.
148        </p></td></tr>
149</table></div>
150<div class="informaltable"><table class="table">
151<colgroup>
152<col>
153<col>
154<col>
155</colgroup>
156<thead><tr>
157<th>
158                <p>
159                  <span class="emphasis"><em><span class="bold"><strong>Synopsis Complexities</strong></span></em></span>
160                </p>
161              </th>
162<th>
163                <p>
164                  interval<br> sets
165                </p>
166              </th>
167<th>
168                <p>
169                  interval<br> maps
170                </p>
171              </th>
172</tr></thead>
173<tbody>
174<tr>
175<td>
176                <p>
177                  <code class="computeroutput"><span class="identifier">J</span> <span class="identifier">elements_begin</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span></code>
178                </p>
179              </td>
180<td>
181                <p>
182                  <span class="emphasis"><em>O(1)</em></span>
183                </p>
184              </td>
185<td>
186                <p>
187                  <span class="emphasis"><em>O(1)</em></span>
188                </p>
189              </td>
190</tr>
191<tr>
192<td>
193                <p>
194                  <code class="computeroutput"><span class="identifier">J</span> <span class="identifier">elements_end</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span></code>
195                </p>
196              </td>
197<td>
198                <p>
199                  <span class="emphasis"><em>O(1)</em></span>
200                </p>
201              </td>
202<td>
203                <p>
204                  <span class="emphasis"><em>O(1)</em></span>
205                </p>
206              </td>
207</tr>
208<tr>
209<td>
210                <p>
211                  <code class="computeroutput"><span class="identifier">J</span> <span class="identifier">elements_rbegin</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span></code>
212                </p>
213              </td>
214<td>
215                <p>
216                  <span class="emphasis"><em>O(1)</em></span>
217                </p>
218              </td>
219<td>
220                <p>
221                  <span class="emphasis"><em>O(1)</em></span>
222                </p>
223              </td>
224</tr>
225<tr>
226<td>
227                <p>
228                  <code class="computeroutput"><span class="identifier">J</span> <span class="identifier">elements_rend</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span></code>
229                </p>
230              </td>
231<td>
232                <p>
233                  <span class="emphasis"><em>O(1)</em></span>
234                </p>
235              </td>
236<td>
237                <p>
238                  <span class="emphasis"><em>O(1)</em></span>
239                </p>
240              </td>
241</tr>
242</tbody>
243</table></div>
244<div class="informaltable"><table class="table">
245<colgroup>
246<col>
247<col>
248</colgroup>
249<thead><tr>
250<th>
251                <p>
252                  <span class="emphasis"><em><span class="bold"><strong>Element iteration</strong></span></em></span>
253                </p>
254              </th>
255<th>
256                <p>
257                  Description
258                </p>
259              </th>
260</tr></thead>
261<tbody>
262<tr>
263<td>
264                <p>
265</p>
266<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">      <span class="identifier">element_iterator</span> <span class="identifier">elements_begin</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span>
267<span class="identifier">element_const_iterator</span> <span class="identifier">elements_begin</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span></pre>
268<p>
269                </p>
270              </td>
271<td>
272                <p>
273                  Returns an element iterator to the first element of the container.
274                </p>
275              </td>
276</tr>
277<tr>
278<td>
279                <p>
280</p>
281<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">      <span class="identifier">element_iterator</span> <span class="identifier">elements_end</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span>
282<span class="identifier">element_const_iterator</span> <span class="identifier">elements_end</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span></pre>
283<p>
284                </p>
285              </td>
286<td>
287                <p>
288                  Returns an element iterator to a position <code class="computeroutput"><span class="identifier">elements_end</span><span class="special">(</span><span class="identifier">c</span><span class="special">)</span></code> after the last element of the
289                  container.
290                </p>
291              </td>
292</tr>
293<tr>
294<td>
295                <p>
296</p>
297<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">      <span class="identifier">element_reverse_iterator</span> <span class="identifier">elements_rbegin</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span>
298<span class="identifier">element_const_reverse_iterator</span> <span class="identifier">elements_rbegin</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span></pre>
299<p>
300                </p>
301              </td>
302<td>
303                <p>
304                  Returns a reverse element iterator to the last element of the container.
305                </p>
306              </td>
307</tr>
308<tr>
309<td>
310                <p>
311</p>
312<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting">      <span class="identifier">element_reverse_iterator</span> <span class="identifier">elements_rend</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;)</span>
313<span class="identifier">element_const_reverse_iterator</span> <span class="identifier">elements_rend</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;)</span></pre>
314<p>
315                </p>
316              </td>
317<td>
318                <p>
319                  Returns a reverse element iterator to a position <code class="computeroutput"><span class="identifier">elements_rend</span><span class="special">(</span><span class="identifier">c</span><span class="special">)</span></code>
320                  before the first element of the container.
321                </p>
322              </td>
323</tr>
324</tbody>
325</table></div>
326<p>
327        <span class="emphasis"><em><span class="bold"><strong>Example</strong></span></em></span>
328      </p>
329<p>
330</p>
331<pre class="programlisting"><span class="identifier">interval_set</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">inter_set</span><span class="special">;</span>
332<span class="identifier">inter_set</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span><span class="identifier">interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">0</span><span class="special">,</span><span class="number">3</span><span class="special">))</span>
333         <span class="special">.</span><span class="identifier">add</span><span class="special">(</span><span class="identifier">interval</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">right_open</span><span class="special">(</span><span class="number">7</span><span class="special">,</span><span class="number">9</span><span class="special">));</span>
334
335<span class="keyword">for</span><span class="special">(</span><span class="identifier">interval_set</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">element_const_iterator</span> <span class="identifier">creeper</span> <span class="special">=</span> <span class="identifier">elements_begin</span><span class="special">(</span><span class="identifier">inter_set</span><span class="special">);</span>
336    <span class="identifier">creeper</span> <span class="special">!=</span> <span class="identifier">elements_end</span><span class="special">(</span><span class="identifier">inter_set</span><span class="special">);</span> <span class="special">++</span><span class="identifier">creeper</span><span class="special">)</span>
337    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">creeper</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span>
338<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
339<span class="comment">//Program output: 0 1 2 7 8</span>
340
341<span class="keyword">for</span><span class="special">(</span><span class="identifier">interval_set</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">element_reverse_iterator</span> <span class="identifier">repeerc</span> <span class="special">=</span> <span class="identifier">elements_rbegin</span><span class="special">(</span><span class="identifier">inter_set</span><span class="special">);</span>
342    <span class="identifier">repeerc</span> <span class="special">!=</span> <span class="identifier">elements_rend</span><span class="special">(</span><span class="identifier">inter_set</span><span class="special">);</span> <span class="special">++</span><span class="identifier">repeerc</span><span class="special">)</span>
343    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">repeerc</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span>
344<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
345<span class="comment">//Program output: 8 7 2 1 0</span>
346</pre>
347<p>
348      </p>
349<p>
350        <span class="emphasis"><em><span class="bold"><strong>See also . . .</strong></span></em></span>
351      </p>
352<div class="informaltable"><table class="table">
353<colgroup><col></colgroup>
354<thead><tr></tr></thead>
355<tbody><tr><td>
356                <p>
357                  <a class="link" href="iterator_related.html" title="Iterator related"><span class="emphasis"><em><span class="bold"><strong>Segment iteration</strong></span></em></span></a>
358                </p>
359              </td></tr></tbody>
360</table></div>
361<p>
362        <span class="emphasis"><em><span class="bold"><strong>Back to section . . .</strong></span></em></span>
363      </p>
364<div class="informaltable"><table class="table">
365<colgroup><col></colgroup>
366<thead><tr></tr></thead>
367<tbody>
368<tr><td>
369                <p>
370                  <a class="link" href="../interface/function_synopsis.html#function_synopsis_table"><span class="emphasis"><em><span class="bold"><strong>Function
371                  Synopsis</strong></span></em></span></a>
372                </p>
373              </td></tr>
374<tr><td>
375                <p>
376                  <a class="link" href="../interface.html" title="Interface"><span class="emphasis"><em><span class="bold"><strong>Interface</strong></span></em></span></a>
377                </p>
378              </td></tr>
379</tbody>
380</table></div>
381</div>
382<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
383<td align="left"></td>
384<td align="right"><div class="copyright-footer">Copyright © 2007-2010 Joachim
385      Faulhaber<br>Copyright © 1999-2006 Cortex Software
386      GmbH<p>
387        Distributed under the Boost Software License, Version 1.0. (See accompanying
388        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
389      </p>
390</div></td>
391</tr></table>
392<hr>
393<div class="spirit-nav">
394<a accesskey="p" href="iterator_related.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_reference.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="streaming__conversion.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
395</div>
396</body>
397</html>
398