Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/utility/generator_iterator.htm @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.9 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
2
3<html>
4<head>
5<title>Generator Iterator Adaptor Documentation</title>
6</head>
7
8<body bgcolor="#FFFFFF" text="#000000">
9       
10<img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" width="277" height="86"> 
11
12<h1>Generator Iterator Adaptor</h1>
13Defined in header <a href="../../boost/generator_iterator.hpp">boost/generator_iterator.hpp</a> 
14<p>
15The generator iterator adaptor makes it easier to create custom input
16iterators from 0-ary functions and function objects.  The adaptor
17takes a
18<a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a>
19and creates a model of
20<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
21Each increment retrieves an item from the generator and makes it
22available to be retrieved by dereferencing.  The motivation for this
23iterator is that some concepts can be more naturally expressed as a
24generator, while most STL algorithms expect an iterator.  An example
25is the <a href="../random/index.html">Random Number</a> library.
26
27<h2>Synopsis</h2>
28
29<blockquote>
30<pre>
31namespace boost {
32  template &lt;class Generator&gt;
33  class generator_iterator_policies;
34
35  template &lt;class Generator&gt;
36  class generator_iterator_generator;
37
38  template &lt;class Generator&gt;
39  typename generator_iterator_generator&lt;Generator&gt;::type
40  make_generator_iterator(Generator &amp; gen);
41}
42</pre>
43</blockquote>
44
45<hr>
46
47<h2>The Generator Iterator Generator Class</h2>
48
49The class generator_iterator_generator is a helper class whose purpose
50is to construct a generator iterator type. The template parameter for
51this class is the Generator function object type that is being
52wrapped.  The generator iterator adaptor only holds a reference (or
53pointer) to the function object, therefore the function object must
54outlive the generator iterator adaptor constructed from it.
55
56<pre>
57template &lt;class Generator>
58class generator_iterator_generator
59{
60public:
61  typedef <i>unspecified</i> type; // the resulting generator iterator type
62}
63</pre>
64
65
66<h3>Template Parameters</h3>
67
68<table border>
69<tr>
70<th>Parameter</th>
71<th>Description</th>
72</tr>
73
74<tr>
75<td><tt><a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a></tt> 
76<td>The generator (0-ary function object) type being
77wrapped.  The return type of the function must be defined as
78<tt>Generator::result_type</tt>.  The function object must be a model
79of
80<a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a>.
81</td>
82</table>
83
84<h3>Concept Model</h3>
85The generator iterator class is a model of
86<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
87
88<h3>Members</h3>
89The generator iterator implements the member functions
90and operators required of the
91<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
92concept.
93
94<br>
95
96<hr>
97<h2><a name="make_generator_iterator">The Generator Iterator Object Generator</a></h2>
98
99The <tt>make_generator_iterator()</tt> function provides a
100convenient way to create generator iterator objects. The function
101saves the user the trouble of explicitly writing out the iterator
102types.
103
104<blockquote>
105<pre>
106template &lt;class Generator&gt;
107typename generator_iterator_generator&lt;Generator&gt;::type
108make_generator_iterator(Generator &amp; gen);
109</pre>
110</blockquote>
111
112<hr>
113
114
115<h3>Example</h3>
116
117The following program shows how <code>generator_iterator</code>
118transforms a generator into an input iterator.
119
120<blockquote>
121<pre>
122#include &lt;iostream>
123#include &lt;boost/generator_iterator.hpp>
124
125class my_generator
126{
127public:
128  typedef int result_type;
129  my_generator() : state(0) { }
130  int operator()() { return ++state; }
131private:
132  int state;
133};
134
135int main()
136{
137  my_generator gen;
138  boost::generator_iterator_generator&lt;my_generator&gt;::type it = boost::make_generator_iterator(gen);
139  for(int i = 0; i &lt; 10; ++i, ++it)
140    std::cout &lt;&lt; *it &lt;&lt; std::endl;
141}
142</pre>
143</blockquote>
144
145<hr>
146
147Written by Jens Maurer.
148
149</body>
150</html>
Note: See TracBrowser for help on using the repository browser.