counting_iterator.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
  7. <title>Counting Iterator</title>
  8. <meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="counting-iterator">
  16. <h1 class="title">Counting Iterator</h1>
  17. <table class="docinfo" frame="void" rules="none">
  18. <col class="docinfo-name" />
  19. <col class="docinfo-content" />
  20. <tbody valign="top">
  21. <tr><th class="docinfo-name">Author:</th>
  22. <td>David Abrahams, Jeremy Siek, Thomas Witt</td></tr>
  23. <tr><th class="docinfo-name">Contact:</th>
  24. <td><a class="first reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="reference external" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a>, <a class="last reference external" href="mailto:witt&#64;ive.uni-hannover.de">witt&#64;ive.uni-hannover.de</a></td></tr>
  25. <tr><th class="docinfo-name">Organization:</th>
  26. <td><a class="first reference external" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University <a class="reference external" href="http://www.osl.iu.edu">Open Systems
  27. Lab</a>, University of Hanover <a class="last reference external" href="http://www.ive.uni-hannover.de">Institute for Transport
  28. Railway Operation and Construction</a></td></tr>
  29. <tr><th class="docinfo-name">Date:</th>
  30. <td>2006-09-11</td></tr>
  31. <tr><th class="docinfo-name">Copyright:</th>
  32. <td>Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.</td></tr>
  33. </tbody>
  34. </table>
  35. <!-- Distributed under the Boost -->
  36. <!-- Software License, Version 1.0. (See accompanying -->
  37. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  38. <table class="docutils field-list" frame="void" rules="none">
  39. <col class="field-name" />
  40. <col class="field-body" />
  41. <tbody valign="top">
  42. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"><p class="first">How would you fill up a vector with the numbers zero
  43. through one hundred using <tt class="docutils literal"><span class="pre">std::copy()</span></tt>? The only iterator
  44. operation missing from builtin integer types is an
  45. <tt class="docutils literal"><span class="pre">operator*()</span></tt> that returns the current value of the integer.
  46. The counting iterator adaptor adds this crucial piece of
  47. functionality to whatever type it wraps. One can use the
  48. counting iterator adaptor not only with integer types, but with
  49. any incrementable type.</p>
  50. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  51. <!-- Software License, Version 1.0. (See accompanying -->
  52. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  53. <p class="last"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> adapts an object by adding an <tt class="docutils literal"><span class="pre">operator*</span></tt> that
  54. returns the current value of the object. All other iterator operations
  55. are forwarded to the adapted object.</p>
  56. </td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. <div class="contents topic" id="table-of-contents">
  61. <p class="topic-title first">Table of Contents</p>
  62. <ul class="simple">
  63. <li><a class="reference internal" href="#counting-iterator-synopsis" id="id2"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> synopsis</a></li>
  64. <li><a class="reference internal" href="#counting-iterator-requirements" id="id3"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> requirements</a></li>
  65. <li><a class="reference internal" href="#counting-iterator-models" id="id4"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models</a></li>
  66. <li><a class="reference internal" href="#counting-iterator-operations" id="id5"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> operations</a></li>
  67. <li><a class="reference internal" href="#example" id="id6">Example</a></li>
  68. </ul>
  69. </div>
  70. <div class="section" id="counting-iterator-synopsis">
  71. <h1><a class="toc-backref" href="#id2"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> synopsis</a></h1>
  72. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  73. <!-- Software License, Version 1.0. (See accompanying -->
  74. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  75. <pre class="literal-block">
  76. template &lt;
  77. class Incrementable
  78. , class CategoryOrTraversal = use_default
  79. , class Difference = use_default
  80. &gt;
  81. class counting_iterator
  82. {
  83. public:
  84. typedef Incrementable value_type;
  85. typedef const Incrementable&amp; reference;
  86. typedef const Incrementable* pointer;
  87. typedef /* see below */ difference_type;
  88. typedef /* see below */ iterator_category;
  89. counting_iterator();
  90. counting_iterator(counting_iterator const&amp; rhs);
  91. explicit counting_iterator(Incrementable x);
  92. Incrementable const&amp; base() const;
  93. reference operator*() const;
  94. counting_iterator&amp; operator++();
  95. counting_iterator&amp; operator--();
  96. private:
  97. Incrementable m_inc; // exposition
  98. };
  99. </pre>
  100. <p>If the <tt class="docutils literal"><span class="pre">Difference</span></tt> argument is <tt class="docutils literal"><span class="pre">use_default</span></tt> then
  101. <tt class="docutils literal"><span class="pre">difference_type</span></tt> is an unspecified signed integral
  102. type. Otherwise <tt class="docutils literal"><span class="pre">difference_type</span></tt> is <tt class="docutils literal"><span class="pre">Difference</span></tt>.</p>
  103. <p><tt class="docutils literal"><span class="pre">iterator_category</span></tt> is determined according to the following
  104. algorithm:</p>
  105. <pre class="literal-block">
  106. if (CategoryOrTraversal is not use_default)
  107. return CategoryOrTraversal
  108. else if (numeric_limits&lt;Incrementable&gt;::is_specialized)
  109. return <a class="reference external" href="iterator_facade.html#iterator-category"><em>iterator-category</em></a>(
  110. random_access_traversal_tag, Incrementable, const Incrementable&amp;)
  111. else
  112. return <a class="reference external" href="iterator_facade.html#iterator-category"><em>iterator-category</em></a>(
  113. iterator_traversal&lt;Incrementable&gt;::type,
  114. Incrementable, const Incrementable&amp;)
  115. </pre>
  116. <dl class="docutils">
  117. <dt>[<em>Note:</em> implementers are encouraged to provide an implementation of</dt>
  118. <dd><tt class="docutils literal"><span class="pre">operator-</span></tt> and a <tt class="docutils literal"><span class="pre">difference_type</span></tt> that avoids overflows in
  119. the cases where <tt class="docutils literal"><span class="pre">std::numeric_limits&lt;Incrementable&gt;::is_specialized</span></tt>
  120. is true.]</dd>
  121. </dl>
  122. </div>
  123. <div class="section" id="counting-iterator-requirements">
  124. <h1><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> requirements</a></h1>
  125. <p>The <tt class="docutils literal"><span class="pre">Incrementable</span></tt> argument shall be Copy Constructible and Assignable.</p>
  126. <p>If <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to <tt class="docutils literal"><span class="pre">forward_iterator_tag</span></tt>
  127. or <tt class="docutils literal"><span class="pre">forward_traversal_tag</span></tt>, the following must be well-formed:</p>
  128. <pre class="literal-block">
  129. Incrementable i, j;
  130. ++i; // pre-increment
  131. i == j; // operator equal
  132. </pre>
  133. <p>If <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  134. <tt class="docutils literal"><span class="pre">bidirectional_iterator_tag</span></tt> or <tt class="docutils literal"><span class="pre">bidirectional_traversal_tag</span></tt>,
  135. the following expression must also be well-formed:</p>
  136. <pre class="literal-block">
  137. --i
  138. </pre>
  139. <p>If <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  140. <tt class="docutils literal"><span class="pre">random_access_iterator_tag</span></tt> or <tt class="docutils literal"><span class="pre">random_access_traversal_tag</span></tt>,
  141. the following must must also be valid:</p>
  142. <pre class="literal-block">
  143. counting_iterator::difference_type n;
  144. i += n;
  145. n = i - j;
  146. i &lt; j;
  147. </pre>
  148. </div>
  149. <div class="section" id="counting-iterator-models">
  150. <h1><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models</a></h1>
  151. <p>Specializations of <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> model Readable Lvalue
  152. Iterator. In addition, they model the concepts corresponding to the
  153. iterator tags to which their <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible.
  154. Also, if <tt class="docutils literal"><span class="pre">CategoryOrTraversal</span></tt> is not <tt class="docutils literal"><span class="pre">use_default</span></tt> then
  155. <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models the concept corresponding to the iterator
  156. tag <tt class="docutils literal"><span class="pre">CategoryOrTraversal</span></tt>. Otherwise, if
  157. <tt class="docutils literal"><span class="pre">numeric_limits&lt;Incrementable&gt;::is_specialized</span></tt>, then
  158. <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models Random Access Traversal Iterator.
  159. Otherwise, <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models the same iterator traversal
  160. concepts modeled by <tt class="docutils literal"><span class="pre">Incrementable</span></tt>.</p>
  161. <p><tt class="docutils literal"><span class="pre">counting_iterator&lt;X,C1,D1&gt;</span></tt> is interoperable with
  162. <tt class="docutils literal"><span class="pre">counting_iterator&lt;Y,C2,D2&gt;</span></tt> if and only if <tt class="docutils literal"><span class="pre">X</span></tt> is
  163. interoperable with <tt class="docutils literal"><span class="pre">Y</span></tt>.</p>
  164. </div>
  165. <div class="section" id="counting-iterator-operations">
  166. <h1><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> operations</a></h1>
  167. <p>In addition to the operations required by the concepts modeled by
  168. <tt class="docutils literal"><span class="pre">counting_iterator</span></tt>, <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> provides the following
  169. operations.</p>
  170. <p><tt class="docutils literal"><span class="pre">counting_iterator();</span></tt></p>
  171. <table class="docutils field-list" frame="void" rules="none">
  172. <col class="field-name" />
  173. <col class="field-body" />
  174. <tbody valign="top">
  175. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Incrementable</span></tt> is Default Constructible.</td>
  176. </tr>
  177. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Default construct the member <tt class="docutils literal"><span class="pre">m_inc</span></tt>.</td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. <p><tt class="docutils literal"><span class="pre">counting_iterator(counting_iterator</span> <span class="pre">const&amp;</span> <span class="pre">rhs);</span></tt></p>
  182. <table class="docutils field-list" frame="void" rules="none">
  183. <col class="field-name" />
  184. <col class="field-body" />
  185. <tbody valign="top">
  186. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Construct member <tt class="docutils literal"><span class="pre">m_inc</span></tt> from <tt class="docutils literal"><span class="pre">rhs.m_inc</span></tt>.</td>
  187. </tr>
  188. </tbody>
  189. </table>
  190. <p><tt class="docutils literal"><span class="pre">explicit</span> <span class="pre">counting_iterator(Incrementable</span> <span class="pre">x);</span></tt></p>
  191. <table class="docutils field-list" frame="void" rules="none">
  192. <col class="field-name" />
  193. <col class="field-body" />
  194. <tbody valign="top">
  195. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Construct member <tt class="docutils literal"><span class="pre">m_inc</span></tt> from <tt class="docutils literal"><span class="pre">x</span></tt>.</td>
  196. </tr>
  197. </tbody>
  198. </table>
  199. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  200. <table class="docutils field-list" frame="void" rules="none">
  201. <col class="field-name" />
  202. <col class="field-body" />
  203. <tbody valign="top">
  204. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_inc</span></tt></td>
  205. </tr>
  206. </tbody>
  207. </table>
  208. <p><tt class="docutils literal"><span class="pre">counting_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  209. <table class="docutils field-list" frame="void" rules="none">
  210. <col class="field-name" />
  211. <col class="field-body" />
  212. <tbody valign="top">
  213. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">++m_inc</span></tt></td>
  214. </tr>
  215. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  216. </tr>
  217. </tbody>
  218. </table>
  219. <p><tt class="docutils literal"><span class="pre">counting_iterator&amp;</span> <span class="pre">operator--();</span></tt></p>
  220. <table class="docutils field-list" frame="void" rules="none">
  221. <col class="field-name" />
  222. <col class="field-body" />
  223. <tbody valign="top">
  224. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">--m_inc</span></tt></td>
  225. </tr>
  226. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  227. </tr>
  228. </tbody>
  229. </table>
  230. <p><tt class="docutils literal"><span class="pre">Incrementable</span> <span class="pre">const&amp;</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
  231. <table class="docutils field-list" frame="void" rules="none">
  232. <col class="field-name" />
  233. <col class="field-body" />
  234. <tbody valign="top">
  235. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_inc</span></tt></td>
  236. </tr>
  237. </tbody>
  238. </table>
  239. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  240. <!-- Software License, Version 1.0. (See accompanying -->
  241. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  242. <pre class="literal-block">
  243. template &lt;class Incrementable&gt;
  244. counting_iterator&lt;Incrementable&gt; make_counting_iterator(Incrementable x);
  245. </pre>
  246. <table class="docutils field-list" frame="void" rules="none">
  247. <col class="field-name" />
  248. <col class="field-body" />
  249. <tbody valign="top">
  250. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">counting_iterator&lt;Incrementable&gt;</span></tt>
  251. with <tt class="docutils literal"><span class="pre">current</span></tt> constructed from <tt class="docutils literal"><span class="pre">x</span></tt>.</td>
  252. </tr>
  253. </tbody>
  254. </table>
  255. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  256. <!-- Software License, Version 1.0. (See accompanying -->
  257. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  258. </div>
  259. <div class="section" id="example">
  260. <h1><a class="toc-backref" href="#id6">Example</a></h1>
  261. <p>This example fills an array with numbers and a second array with
  262. pointers into the first array, using <tt class="docutils literal"><span class="pre">counting_iterator</span></tt> for both
  263. tasks. Finally <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> is used to print out the numbers
  264. into the first array via indirection through the second array.</p>
  265. <pre class="literal-block">
  266. int N = 7;
  267. std::vector&lt;int&gt; numbers;
  268. typedef std::vector&lt;int&gt;::iterator n_iter;
  269. std::copy(boost::counting_iterator&lt;int&gt;(0),
  270. boost::counting_iterator&lt;int&gt;(N),
  271. std::back_inserter(numbers));
  272. std::vector&lt;std::vector&lt;int&gt;::iterator&gt; pointers;
  273. std::copy(boost::make_counting_iterator(numbers.begin()),
  274. boost::make_counting_iterator(numbers.end()),
  275. std::back_inserter(pointers));
  276. std::cout &lt;&lt; &quot;indirectly printing out the numbers from 0 to &quot;
  277. &lt;&lt; N &lt;&lt; std::endl;
  278. std::copy(boost::make_indirect_iterator(pointers.begin()),
  279. boost::make_indirect_iterator(pointers.end()),
  280. std::ostream_iterator&lt;int&gt;(std::cout, &quot; &quot;));
  281. std::cout &lt;&lt; std::endl;
  282. </pre>
  283. <p>The output is:</p>
  284. <pre class="literal-block">
  285. indirectly printing out the numbers from 0 to 7
  286. 0 1 2 3 4 5 6
  287. </pre>
  288. <p>The source code for this example can be found <a class="reference external" href="../example/counting_iterator_example.cpp">here</a>.</p>
  289. </div>
  290. </div>
  291. <div class="footer">
  292. <hr class="footer" />
  293. <a class="reference external" href="counting_iterator.rst">View document source</a>.
  294. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  295. </div>
  296. </body>
  297. </html>