filter_iterator.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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>Filter 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="filter-iterator">
  16. <h1 class="title">Filter 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 filter iterator adaptor creates a view of an iterator range in
  46. which some elements of the range are skipped. A predicate function
  47. object controls which elements are skipped. When the predicate is
  48. applied to an element, if it returns <tt class="docutils literal"><span class="pre">true</span></tt> then the element is
  49. retained and if it returns <tt class="docutils literal"><span class="pre">false</span></tt> then the element is skipped
  50. over. When skipping over elements, it is necessary for the filter
  51. adaptor to know when to stop so as to avoid going past the end of the
  52. underlying range. A filter iterator is therefore constructed with pair
  53. of iterators indicating the range of elements in the unfiltered
  54. sequence to be traversed.</td>
  55. </tr>
  56. </tbody>
  57. </table>
  58. <div class="contents topic" id="table-of-contents">
  59. <p class="topic-title first">Table of Contents</p>
  60. <ul class="simple">
  61. <li><a class="reference internal" href="#filter-iterator-synopsis" id="id2"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> synopsis</a></li>
  62. <li><a class="reference internal" href="#filter-iterator-requirements" id="id3"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> requirements</a></li>
  63. <li><a class="reference internal" href="#filter-iterator-models" id="id4"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</a></li>
  64. <li><a class="reference internal" href="#filter-iterator-operations" id="id5"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> operations</a></li>
  65. <li><a class="reference internal" href="#example" id="id6">Example</a></li>
  66. </ul>
  67. </div>
  68. <div class="section" id="filter-iterator-synopsis">
  69. <h1><a class="toc-backref" href="#id2"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> synopsis</a></h1>
  70. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt -->
  71. <!-- 2004. Use, modification and distribution is subject to the Boost -->
  72. <!-- Software License, Version 1.0. (See accompanying file -->
  73. <!-- LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  74. <pre class="literal-block">
  75. template &lt;class Predicate, class Iterator&gt;
  76. class filter_iterator
  77. {
  78. public:
  79. typedef iterator_traits&lt;Iterator&gt;::value_type value_type;
  80. typedef iterator_traits&lt;Iterator&gt;::reference reference;
  81. typedef iterator_traits&lt;Iterator&gt;::pointer pointer;
  82. typedef iterator_traits&lt;Iterator&gt;::difference_type difference_type;
  83. typedef /* see below */ iterator_category;
  84. filter_iterator();
  85. filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
  86. filter_iterator(Iterator x, Iterator end = Iterator());
  87. template&lt;class OtherIterator&gt;
  88. filter_iterator(
  89. filter_iterator&lt;Predicate, OtherIterator&gt; const&amp; t
  90. , typename enable_if_convertible&lt;OtherIterator, Iterator&gt;::type* = 0 // exposition
  91. );
  92. Predicate predicate() const;
  93. Iterator end() const;
  94. Iterator const&amp; base() const;
  95. reference operator*() const;
  96. filter_iterator&amp; operator++();
  97. private:
  98. Predicate m_pred; // exposition only
  99. Iterator m_iter; // exposition only
  100. Iterator m_end; // exposition only
  101. };
  102. </pre>
  103. <p>If <tt class="docutils literal"><span class="pre">Iterator</span></tt> models Readable Lvalue Iterator and Bidirectional Traversal
  104. Iterator then <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  105. <tt class="docutils literal"><span class="pre">std::bidirectional_iterator_tag</span></tt>.
  106. Otherwise, if <tt class="docutils literal"><span class="pre">Iterator</span></tt> models Readable Lvalue Iterator and Forward Traversal
  107. Iterator then <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  108. <tt class="docutils literal"><span class="pre">std::forward_iterator_tag</span></tt>.
  109. Otherwise <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is
  110. convertible to <tt class="docutils literal"><span class="pre">std::input_iterator_tag</span></tt>.</p>
  111. </div>
  112. <div class="section" id="filter-iterator-requirements">
  113. <h1><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> requirements</a></h1>
  114. <p>The <tt class="docutils literal"><span class="pre">Iterator</span></tt> argument shall meet the requirements of Readable
  115. Iterator and Single Pass Iterator or it shall meet the requirements of
  116. Input Iterator.</p>
  117. <p>The <tt class="docutils literal"><span class="pre">Predicate</span></tt> argument must be Assignable, Copy Constructible, and
  118. the expression <tt class="docutils literal"><span class="pre">p(x)</span></tt> must be valid where <tt class="docutils literal"><span class="pre">p</span></tt> is an object of type
  119. <tt class="docutils literal"><span class="pre">Predicate</span></tt>, <tt class="docutils literal"><span class="pre">x</span></tt> is an object of type
  120. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::value_type</span></tt>, and where the type of
  121. <tt class="docutils literal"><span class="pre">p(x)</span></tt> must be convertible to <tt class="docutils literal"><span class="pre">bool</span></tt>.</p>
  122. </div>
  123. <div class="section" id="filter-iterator-models">
  124. <h1><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</a></h1>
  125. <p>The concepts that <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models are dependent on which
  126. concepts the <tt class="docutils literal"><span class="pre">Iterator</span></tt> argument models, as specified in the
  127. following tables.</p>
  128. <table border="1" class="docutils">
  129. <colgroup>
  130. <col width="44%" />
  131. <col width="56%" />
  132. </colgroup>
  133. <thead valign="bottom">
  134. <tr><th class="head">If <tt class="docutils literal"><span class="pre">Iterator</span></tt> models</th>
  135. <th class="head">then <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</th>
  136. </tr>
  137. </thead>
  138. <tbody valign="top">
  139. <tr><td>Single Pass Iterator</td>
  140. <td>Single Pass Iterator</td>
  141. </tr>
  142. <tr><td>Forward Traversal Iterator</td>
  143. <td>Forward Traversal Iterator</td>
  144. </tr>
  145. <tr><td>Bidirectional Traversal Iterator</td>
  146. <td>Bidirectional Traversal Iterator</td>
  147. </tr>
  148. </tbody>
  149. </table>
  150. <table border="1" class="docutils">
  151. <colgroup>
  152. <col width="41%" />
  153. <col width="59%" />
  154. </colgroup>
  155. <thead valign="bottom">
  156. <tr><th class="head">If <tt class="docutils literal"><span class="pre">Iterator</span></tt> models</th>
  157. <th class="head">then <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</th>
  158. </tr>
  159. </thead>
  160. <tbody valign="top">
  161. <tr><td>Readable Iterator</td>
  162. <td>Readable Iterator</td>
  163. </tr>
  164. <tr><td>Writable Iterator</td>
  165. <td>Writable Iterator</td>
  166. </tr>
  167. <tr><td>Lvalue Iterator</td>
  168. <td>Lvalue Iterator</td>
  169. </tr>
  170. </tbody>
  171. </table>
  172. <table border="1" class="docutils">
  173. <colgroup>
  174. <col width="63%" />
  175. <col width="38%" />
  176. </colgroup>
  177. <thead valign="bottom">
  178. <tr><th class="head">If <tt class="docutils literal"><span class="pre">Iterator</span></tt> models</th>
  179. <th class="head">then <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</th>
  180. </tr>
  181. </thead>
  182. <tbody valign="top">
  183. <tr><td>Readable Iterator, Single Pass Iterator</td>
  184. <td>Input Iterator</td>
  185. </tr>
  186. <tr><td>Readable Lvalue Iterator, Forward Traversal Iterator</td>
  187. <td>Forward Iterator</td>
  188. </tr>
  189. <tr><td>Writable Lvalue Iterator, Forward Traversal Iterator</td>
  190. <td>Mutable Forward Iterator</td>
  191. </tr>
  192. <tr><td>Writable Lvalue Iterator, Bidirectional Iterator</td>
  193. <td>Mutable Bidirectional Iterator</td>
  194. </tr>
  195. </tbody>
  196. </table>
  197. <p><tt class="docutils literal"><span class="pre">filter_iterator&lt;P1,</span> <span class="pre">X&gt;</span></tt> is interoperable with <tt class="docutils literal"><span class="pre">filter_iterator&lt;P2,</span> <span class="pre">Y&gt;</span></tt>
  198. if and only if <tt class="docutils literal"><span class="pre">X</span></tt> is interoperable with <tt class="docutils literal"><span class="pre">Y</span></tt>.</p>
  199. </div>
  200. <div class="section" id="filter-iterator-operations">
  201. <h1><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> operations</a></h1>
  202. <p>In addition to those operations required by the concepts that
  203. <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models, <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> provides the following
  204. operations.</p>
  205. <p><tt class="docutils literal"><span class="pre">filter_iterator();</span></tt></p>
  206. <table class="docutils field-list" frame="void" rules="none">
  207. <col class="field-name" />
  208. <col class="field-body" />
  209. <tbody valign="top">
  210. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Predicate</span></tt> and <tt class="docutils literal"><span class="pre">Iterator</span></tt> must be Default Constructible.</td>
  211. </tr>
  212. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs a <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> whose``m_pred``, <tt class="docutils literal"><span class="pre">m_iter</span></tt>, and <tt class="docutils literal"><span class="pre">m_end</span></tt>
  213. members are a default constructed.</td>
  214. </tr>
  215. </tbody>
  216. </table>
  217. <p><tt class="docutils literal"><span class="pre">filter_iterator(Predicate</span> <span class="pre">f,</span> <span class="pre">Iterator</span> <span class="pre">x,</span> <span class="pre">Iterator</span> <span class="pre">end</span> <span class="pre">=</span> <span class="pre">Iterator());</span></tt></p>
  218. <table class="docutils field-list" frame="void" rules="none">
  219. <col class="field-name" />
  220. <col class="field-body" />
  221. <tbody valign="top">
  222. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs a <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> where <tt class="docutils literal"><span class="pre">m_iter</span></tt> is either
  223. the first position in the range <tt class="docutils literal"><span class="pre">[x,end)</span></tt> such that <tt class="docutils literal"><span class="pre">f(*m_iter)</span> <span class="pre">==</span> <span class="pre">true</span></tt>
  224. or else``m_iter == end``. The member <tt class="docutils literal"><span class="pre">m_pred</span></tt> is constructed from
  225. <tt class="docutils literal"><span class="pre">f</span></tt> and <tt class="docutils literal"><span class="pre">m_end</span></tt> from <tt class="docutils literal"><span class="pre">end</span></tt>.</td>
  226. </tr>
  227. </tbody>
  228. </table>
  229. <p><tt class="docutils literal"><span class="pre">filter_iterator(Iterator</span> <span class="pre">x,</span> <span class="pre">Iterator</span> <span class="pre">end</span> <span class="pre">=</span> <span class="pre">Iterator());</span></tt></p>
  230. <table class="docutils field-list" frame="void" rules="none">
  231. <col class="field-name" />
  232. <col class="field-body" />
  233. <tbody valign="top">
  234. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Predicate</span></tt> must be Default Constructible and
  235. <tt class="docutils literal"><span class="pre">Predicate</span></tt> is a class type (not a function pointer).</td>
  236. </tr>
  237. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs a <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> where <tt class="docutils literal"><span class="pre">m_iter</span></tt> is either
  238. the first position in the range <tt class="docutils literal"><span class="pre">[x,end)</span></tt> such that <tt class="docutils literal"><span class="pre">m_pred(*m_iter)</span> <span class="pre">==</span> <span class="pre">true</span></tt>
  239. or else``m_iter == end``. The member <tt class="docutils literal"><span class="pre">m_pred</span></tt> is default constructed.</td>
  240. </tr>
  241. </tbody>
  242. </table>
  243. <pre class="literal-block">
  244. template &lt;class OtherIterator&gt;
  245. filter_iterator(
  246. filter_iterator&lt;Predicate, OtherIterator&gt; const&amp; t
  247. , typename enable_if_convertible&lt;OtherIterator, Iterator&gt;::type* = 0 // exposition
  248. );``
  249. </pre>
  250. <table class="docutils field-list" frame="void" rules="none">
  251. <col class="field-name" />
  252. <col class="field-body" />
  253. <tbody valign="top">
  254. <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>
  255. </tr>
  256. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs a filter iterator whose members are copied from <tt class="docutils literal"><span class="pre">t</span></tt>.</td>
  257. </tr>
  258. </tbody>
  259. </table>
  260. <p><tt class="docutils literal"><span class="pre">Predicate</span> <span class="pre">predicate()</span> <span class="pre">const;</span></tt></p>
  261. <table class="docutils field-list" frame="void" rules="none">
  262. <col class="field-name" />
  263. <col class="field-body" />
  264. <tbody valign="top">
  265. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_pred</span></tt></td>
  266. </tr>
  267. </tbody>
  268. </table>
  269. <p><tt class="docutils literal"><span class="pre">Iterator</span> <span class="pre">end()</span> <span class="pre">const;</span></tt></p>
  270. <table class="docutils field-list" frame="void" rules="none">
  271. <col class="field-name" />
  272. <col class="field-body" />
  273. <tbody valign="top">
  274. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_end</span></tt></td>
  275. </tr>
  276. </tbody>
  277. </table>
  278. <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>
  279. <table class="docutils field-list" frame="void" rules="none">
  280. <col class="field-name" />
  281. <col class="field-body" />
  282. <tbody valign="top">
  283. <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>
  284. </tr>
  285. </tbody>
  286. </table>
  287. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  288. <table class="docutils field-list" frame="void" rules="none">
  289. <col class="field-name" />
  290. <col class="field-body" />
  291. <tbody valign="top">
  292. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*m_iter</span></tt></td>
  293. </tr>
  294. </tbody>
  295. </table>
  296. <p><tt class="docutils literal"><span class="pre">filter_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  297. <table class="docutils field-list" frame="void" rules="none">
  298. <col class="field-name" />
  299. <col class="field-body" />
  300. <tbody valign="top">
  301. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Increments <tt class="docutils literal"><span class="pre">m_iter</span></tt> and then continues to
  302. increment <tt class="docutils literal"><span class="pre">m_iter</span></tt> until either <tt class="docutils literal"><span class="pre">m_iter</span> <span class="pre">==</span> <span class="pre">m_end</span></tt>
  303. or <tt class="docutils literal"><span class="pre">m_pred(*m_iter)</span> <span class="pre">==</span> <span class="pre">true</span></tt>.</td>
  304. </tr>
  305. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  306. </tr>
  307. </tbody>
  308. </table>
  309. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  310. <!-- Software License, Version 1.0. (See accompanying -->
  311. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  312. <pre class="literal-block">
  313. template &lt;class Predicate, class Iterator&gt;
  314. filter_iterator&lt;Predicate,Iterator&gt;
  315. make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
  316. </pre>
  317. <table class="docutils field-list" frame="void" rules="none">
  318. <col class="field-name" />
  319. <col class="field-body" />
  320. <tbody valign="top">
  321. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">filter_iterator&lt;Predicate,Iterator&gt;(f, x, end)</td>
  322. </tr>
  323. </tbody>
  324. </table>
  325. <pre class="literal-block">
  326. template &lt;class Predicate, class Iterator&gt;
  327. filter_iterator&lt;Predicate,Iterator&gt;
  328. make_filter_iterator(Iterator x, Iterator end = Iterator());
  329. </pre>
  330. <table class="docutils field-list" frame="void" rules="none">
  331. <col class="field-name" />
  332. <col class="field-body" />
  333. <tbody valign="top">
  334. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">filter_iterator&lt;Predicate,Iterator&gt;(x, end)</td>
  335. </tr>
  336. </tbody>
  337. </table>
  338. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  339. <!-- Software License, Version 1.0. (See accompanying -->
  340. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  341. </div>
  342. <div class="section" id="example">
  343. <h1><a class="toc-backref" href="#id6">Example</a></h1>
  344. <p>This example uses <tt class="docutils literal"><span class="pre">filter_iterator</span></tt> and then
  345. <tt class="docutils literal"><span class="pre">make_filter_iterator</span></tt> to output only the positive integers from an
  346. array of integers. Then <tt class="docutils literal"><span class="pre">make_filter_iterator</span></tt> is is used to output
  347. the integers greater than <tt class="docutils literal"><span class="pre">-2</span></tt>.</p>
  348. <pre class="literal-block">
  349. struct is_positive_number {
  350. bool operator()(int x) { return 0 &lt; x; }
  351. };
  352. int main()
  353. {
  354. int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 };
  355. const int N = sizeof(numbers_)/sizeof(int);
  356. typedef int* base_iterator;
  357. base_iterator numbers(numbers_);
  358. // Example using filter_iterator
  359. typedef boost::filter_iterator&lt;is_positive_number, base_iterator&gt;
  360. FilterIter;
  361. is_positive_number predicate;
  362. FilterIter filter_iter_first(predicate, numbers, numbers + N);
  363. FilterIter filter_iter_last(predicate, numbers + N, numbers + N);
  364. std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator&lt;int&gt;(std::cout, &quot; &quot;));
  365. std::cout &lt;&lt; std::endl;
  366. // Example using make_filter_iterator()
  367. std::copy(boost::make_filter_iterator&lt;is_positive_number&gt;(numbers, numbers + N),
  368. boost::make_filter_iterator&lt;is_positive_number&gt;(numbers + N, numbers + N),
  369. std::ostream_iterator&lt;int&gt;(std::cout, &quot; &quot;));
  370. std::cout &lt;&lt; std::endl;
  371. // Another example using make_filter_iterator()
  372. std::copy(
  373. boost::make_filter_iterator(
  374. std::bind2nd(std::greater&lt;int&gt;(), -2)
  375. , numbers, numbers + N)
  376. , boost::make_filter_iterator(
  377. std::bind2nd(std::greater&lt;int&gt;(), -2)
  378. , numbers + N, numbers + N)
  379. , std::ostream_iterator&lt;int&gt;(std::cout, &quot; &quot;)
  380. );
  381. std::cout &lt;&lt; std::endl;
  382. return boost::exit_success;
  383. }
  384. </pre>
  385. <p>The output is:</p>
  386. <pre class="literal-block">
  387. 4 5 8
  388. 4 5 8
  389. 0 -1 4 5 8
  390. </pre>
  391. <p>The source code for this example can be found <a class="reference external" href="../example/filter_iterator_example.cpp">here</a>.</p>
  392. </div>
  393. </div>
  394. <div class="footer">
  395. <hr class="footer" />
  396. <a class="reference external" href="filter_iterator.rst">View document source</a>.
  397. 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.
  398. </div>
  399. </body>
  400. </html>