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