reverse_iterator.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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>Reverse 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="reverse-iterator">
  16. <h1 class="title">Reverse 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"><!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  43. <!-- Software License, Version 1.0. (See accompanying -->
  44. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  45. The reverse iterator adaptor iterates through the adapted iterator
  46. range in the opposite direction.</td>
  47. </tr>
  48. </tbody>
  49. </table>
  50. <div class="contents topic" id="table-of-contents">
  51. <p class="topic-title first">Table of Contents</p>
  52. <ul class="simple">
  53. <li><a class="reference internal" href="#reverse-iterator-synopsis" id="id2"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> synopsis</a></li>
  54. <li><a class="reference internal" href="#reverse-iterator-requirements" id="id3"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> requirements</a></li>
  55. <li><a class="reference internal" href="#reverse-iterator-models" id="id4"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> models</a></li>
  56. <li><a class="reference internal" href="#reverse-iterator-operations" id="id5"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> operations</a></li>
  57. <li><a class="reference internal" href="#example" id="id6">Example</a></li>
  58. </ul>
  59. </div>
  60. <div class="section" id="reverse-iterator-synopsis">
  61. <h1><a class="toc-backref" href="#id2"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> synopsis</a></h1>
  62. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  63. <!-- Software License, Version 1.0. (See accompanying -->
  64. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  65. <pre class="literal-block">
  66. template &lt;class Iterator&gt;
  67. class reverse_iterator
  68. {
  69. public:
  70. typedef iterator_traits&lt;Iterator&gt;::value_type value_type;
  71. typedef iterator_traits&lt;Iterator&gt;::reference reference;
  72. typedef iterator_traits&lt;Iterator&gt;::pointer pointer;
  73. typedef iterator_traits&lt;Iterator&gt;::difference_type difference_type;
  74. typedef /* see below */ iterator_category;
  75. reverse_iterator() {}
  76. explicit reverse_iterator(Iterator x) ;
  77. template&lt;class OtherIterator&gt;
  78. reverse_iterator(
  79. reverse_iterator&lt;OtherIterator&gt; const&amp; r
  80. , typename enable_if_convertible&lt;OtherIterator, Iterator&gt;::type* = 0 // exposition
  81. );
  82. Iterator const&amp; base() const;
  83. reference operator*() const;
  84. reverse_iterator&amp; operator++();
  85. reverse_iterator&amp; operator--();
  86. private:
  87. Iterator m_iterator; // exposition
  88. };
  89. </pre>
  90. <p>If <tt class="docutils literal"><span class="pre">Iterator</span></tt> models Random Access Traversal Iterator and Readable
  91. Lvalue Iterator, then <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  92. <tt class="docutils literal"><span class="pre">random_access_iterator_tag</span></tt>. Otherwise, if
  93. <tt class="docutils literal"><span class="pre">Iterator</span></tt> models Bidirectional Traversal Iterator and Readable
  94. Lvalue Iterator, then <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  95. <tt class="docutils literal"><span class="pre">bidirectional_iterator_tag</span></tt>. Otherwise, <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is
  96. convertible to <tt class="docutils literal"><span class="pre">input_iterator_tag</span></tt>.</p>
  97. </div>
  98. <div class="section" id="reverse-iterator-requirements">
  99. <h1><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> requirements</a></h1>
  100. <p><tt class="docutils literal"><span class="pre">Iterator</span></tt> must be a model of Bidirectional Traversal Iterator. The
  101. type <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::reference</span></tt> must be the type of
  102. <tt class="docutils literal"><span class="pre">*i</span></tt>, where <tt class="docutils literal"><span class="pre">i</span></tt> is an object of type <tt class="docutils literal"><span class="pre">Iterator</span></tt>.</p>
  103. </div>
  104. <div class="section" id="reverse-iterator-models">
  105. <h1><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> models</a></h1>
  106. <p>A specialization of <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> models the same iterator
  107. traversal and iterator access concepts modeled by its <tt class="docutils literal"><span class="pre">Iterator</span></tt>
  108. argument. In addition, it may model old iterator concepts
  109. specified in the following table:</p>
  110. <table border="1" class="docutils">
  111. <colgroup>
  112. <col width="53%" />
  113. <col width="47%" />
  114. </colgroup>
  115. <thead valign="bottom">
  116. <tr><th class="head">If <tt class="docutils literal"><span class="pre">I</span></tt> models</th>
  117. <th class="head">then <tt class="docutils literal"><span class="pre">reverse_iterator&lt;I&gt;</span></tt> models</th>
  118. </tr>
  119. </thead>
  120. <tbody valign="top">
  121. <tr><td>Readable Lvalue Iterator,
  122. Bidirectional Traversal Iterator</td>
  123. <td>Bidirectional Iterator</td>
  124. </tr>
  125. <tr><td>Writable Lvalue Iterator,
  126. Bidirectional Traversal Iterator</td>
  127. <td>Mutable Bidirectional Iterator</td>
  128. </tr>
  129. <tr><td>Readable Lvalue Iterator,
  130. Random Access Traversal Iterator</td>
  131. <td>Random Access Iterator</td>
  132. </tr>
  133. <tr><td>Writable Lvalue Iterator,
  134. Random Access Traversal Iterator</td>
  135. <td>Mutable Random Access Iterator</td>
  136. </tr>
  137. </tbody>
  138. </table>
  139. <p><tt class="docutils literal"><span class="pre">reverse_iterator&lt;X&gt;</span></tt> is interoperable with
  140. <tt class="docutils literal"><span class="pre">reverse_iterator&lt;Y&gt;</span></tt> if and only if <tt class="docutils literal"><span class="pre">X</span></tt> is interoperable with
  141. <tt class="docutils literal"><span class="pre">Y</span></tt>.</p>
  142. </div>
  143. <div class="section" id="reverse-iterator-operations">
  144. <h1><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> operations</a></h1>
  145. <p>In addition to the operations required by the concepts modeled by
  146. <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt>, <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> provides the following
  147. operations.</p>
  148. <p><tt class="docutils literal"><span class="pre">reverse_iterator();</span></tt></p>
  149. <table class="docutils field-list" frame="void" rules="none">
  150. <col class="field-name" />
  151. <col class="field-body" />
  152. <tbody valign="top">
  153. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Iterator</span></tt> must be Default Constructible.</td>
  154. </tr>
  155. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs an instance of <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> with <tt class="docutils literal"><span class="pre">m_iterator</span></tt>
  156. default constructed.</td>
  157. </tr>
  158. </tbody>
  159. </table>
  160. <p><tt class="docutils literal"><span class="pre">explicit</span> <span class="pre">reverse_iterator(Iterator</span> <span class="pre">x);</span></tt></p>
  161. <table class="docutils field-list" frame="void" rules="none">
  162. <col class="field-name" />
  163. <col class="field-body" />
  164. <tbody valign="top">
  165. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs an instance of <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> with
  166. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> copy constructed from <tt class="docutils literal"><span class="pre">x</span></tt>.</td>
  167. </tr>
  168. </tbody>
  169. </table>
  170. <pre class="literal-block">
  171. template&lt;class OtherIterator&gt;
  172. reverse_iterator(
  173. reverse_iterator&lt;OtherIterator&gt; const&amp; r
  174. , typename enable_if_convertible&lt;OtherIterator, Iterator&gt;::type* = 0 // exposition
  175. );
  176. </pre>
  177. <table class="docutils field-list" frame="void" rules="none">
  178. <col class="field-name" />
  179. <col class="field-body" />
  180. <tbody valign="top">
  181. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">OtherIterator</span></tt> is implicitly convertible to <tt class="docutils literal"><span class="pre">Iterator</span></tt>.</td>
  182. </tr>
  183. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs instance of <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> whose
  184. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> subobject is constructed from <tt class="docutils literal"><span class="pre">y.base()</span></tt>.</td>
  185. </tr>
  186. </tbody>
  187. </table>
  188. <p><tt class="docutils literal"><span class="pre">Iterator</span> <span class="pre">const&amp;</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
  189. <table class="docutils field-list" frame="void" rules="none">
  190. <col class="field-name" />
  191. <col class="field-body" />
  192. <tbody valign="top">
  193. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator</span></tt></td>
  194. </tr>
  195. </tbody>
  196. </table>
  197. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  198. <table class="docutils field-list" frame="void" rules="none">
  199. <col class="field-name" />
  200. <col class="field-body" />
  201. <tbody valign="top">
  202. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"></td>
  203. </tr>
  204. </tbody>
  205. </table>
  206. <pre class="literal-block">
  207. Iterator tmp = m_iterator;
  208. return *--tmp;
  209. </pre>
  210. <p><tt class="docutils literal"><span class="pre">reverse_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  211. <table class="docutils field-list" frame="void" rules="none">
  212. <col class="field-name" />
  213. <col class="field-body" />
  214. <tbody valign="top">
  215. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">--m_iterator</span></tt></td>
  216. </tr>
  217. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  218. </tr>
  219. </tbody>
  220. </table>
  221. <p><tt class="docutils literal"><span class="pre">reverse_iterator&amp;</span> <span class="pre">operator--();</span></tt></p>
  222. <table class="docutils field-list" frame="void" rules="none">
  223. <col class="field-name" />
  224. <col class="field-body" />
  225. <tbody valign="top">
  226. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">++m_iterator</span></tt></td>
  227. </tr>
  228. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  229. </tr>
  230. </tbody>
  231. </table>
  232. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  233. <!-- Software License, Version 1.0. (See accompanying -->
  234. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  235. <pre class="literal-block">
  236. template &lt;class BidirectionalIterator&gt;
  237. reverse_iterator&lt;BidirectionalIterator&gt;n
  238. make_reverse_iterator(BidirectionalIterator x);
  239. </pre>
  240. <table class="docutils field-list" frame="void" rules="none">
  241. <col class="field-name" />
  242. <col class="field-body" />
  243. <tbody valign="top">
  244. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">reverse_iterator&lt;BidirectionalIterator&gt;</span></tt>
  245. with a <tt class="docutils literal"><span class="pre">current</span></tt> constructed from <tt class="docutils literal"><span class="pre">x</span></tt>.</td>
  246. </tr>
  247. </tbody>
  248. </table>
  249. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  250. <!-- Software License, Version 1.0. (See accompanying -->
  251. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  252. </div>
  253. <div class="section" id="example">
  254. <h1><a class="toc-backref" href="#id6">Example</a></h1>
  255. <p>The following example prints an array of characters in reverse order
  256. using <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt>.</p>
  257. <pre class="literal-block">
  258. char letters_[] = &quot;hello world!&quot;;
  259. const int N = sizeof(letters_)/sizeof(char) - 1;
  260. typedef char* base_iterator;
  261. base_iterator letters(letters_);
  262. std::cout &lt;&lt; &quot;original sequence of letters:\t\t\t&quot; &lt;&lt; letters_ &lt;&lt; std::endl;
  263. boost::reverse_iterator&lt;base_iterator&gt;
  264. reverse_letters_first(letters + N),
  265. reverse_letters_last(letters);
  266. std::cout &lt;&lt; &quot;sequence in reverse order:\t\t\t&quot;;
  267. std::copy(reverse_letters_first, reverse_letters_last,
  268. std::ostream_iterator&lt;char&gt;(std::cout));
  269. std::cout &lt;&lt; std::endl;
  270. std::cout &lt;&lt; &quot;sequence in double-reversed (normal) order:\t&quot;;
  271. std::copy(boost::make_reverse_iterator(reverse_letters_last),
  272. boost::make_reverse_iterator(reverse_letters_first),
  273. std::ostream_iterator&lt;char&gt;(std::cout));
  274. std::cout &lt;&lt; std::endl;
  275. </pre>
  276. <p>The output is:</p>
  277. <pre class="literal-block">
  278. original sequence of letters: hello world!
  279. sequence in reverse order: !dlrow olleh
  280. sequence in double-reversed (normal) order: hello world!
  281. </pre>
  282. <p>The source code for this example can be found <a class="reference external" href="../example/reverse_iterator_example.cpp">here</a>.</p>
  283. </div>
  284. </div>
  285. <div class="footer">
  286. <hr class="footer" />
  287. <a class="reference external" href="reverse_iterator.rst">View document source</a>.
  288. 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.
  289. </div>
  290. </body>
  291. </html>