File: function_output_iterator.htm

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (169 lines) | stat: -rw-r--r-- 5,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
    <meta name="generator" content="HTML Tidy, see www.w3.org">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">

    <title>Function Output Iterator Adaptor Documentation</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">
        
    <img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align=
    "center" width="277" height="86"> 

    <h1>Function Output Iterator Adaptor</h1>
    Defined in header <a href=
    "../../boost/function_output_iterator.hpp">boost/function_output_iterator.hpp</a> 

    <p>The function output iterator adaptor makes it easier to create
    custom output iterators. The adaptor takes a <a
    href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary
    Function</a> and creates a model of <a
    href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
    Iterator</a>. Each item assigned to the output iterator is passed
    as an argument to the unary function.  The motivation for this
    iterator is that creating a C++ Standard conforming output
    iterator is non-trivial, particularly because the proper
    implementation usually requires a proxy object. On the other hand,
    creating a function (or function object) is much simpler.

    <h2>Synopsis</h2>

    <blockquote>
<pre>
namespace boost {
  template &lt;class UnaryFunction&gt;
  class function_output_iterator;

  template &lt;class UnaryFunction&gt;
  function_output_iterator&lt;UnaryFunction&gt;
  make_function_output_iterator(const UnaryFunction&amp; f = UnaryFunction())
}
</pre>
    </blockquote>

    <h3>Example</h3>
    
    In this example we create an output iterator that appends
    each item onto the end of a string, using the <tt>string_appender</tt>
    function. 

    <blockquote>
<pre>
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

#include &lt;boost/function_output_iterator.hpp&gt;

struct string_appender {
  string_appender(std::string&amp; s) : m_str(s) { }
  void operator()(const std::string&amp; x) const {
    m_str += x;
  }
  std::string&amp; m_str;
};

int main(int, char*[])
{
  std::vector&lt;std::string&gt; x;
  x.push_back("hello");
  x.push_back(" ");
  x.push_back("world");
  x.push_back("!");

  std::string s = "";
  std::copy(x.begin(), x.end(), 
            boost::make_function_output_iterator(string_appender(s)));
  
  std::cout &lt;&lt; s &lt;&lt; std::endl;

  return 0;
}
</pre>
    </blockquote>

    <hr>

    <h2><a name="function_output_iterator">The Function Output Iterator Class</a></h2>

    <blockquote>
<pre>
template &lt;class UnaryFunction&gt;
class function_output_iterator;
</pre>
    </blockquote>

    The <tt>function_output_iterator</tt> class creates an <a
    href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
    Iterator</a> out of a
    <a href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary
    Function</a>. Each item assigned to the output iterator is passed
    as an argument to the unary function.

    <h3>Template Parameters</h3>

    <table border>
      <tr>
        <th>Parameter

        <th>Description

      <tr>
        <td><tt>UnaryFunction</tt> 

        <td>The function type being wrapped. The return type of the
        function is not used, so it can be <tt>void</tt>.  The
        function must be a model of <a
        href="http://www.sgi.com/tech/stl/UnaryFunction.html">Unary
        Function</a>.</td>
    </table>

    <h3>Concept Model</h3>
    The function output iterator class is a model of <a
    href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
    Iterator</a>.

    <h2>Members</h3>
    The function output iterator implements the member functions
    and operators required of the <a
    href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
    Iterator</a> concept. In addition it has the following constructor:
<pre>
explicit function_output_iterator(const UnaryFunction& f = UnaryFunction())
</pre>
   <br>    
    <br>

    <hr>
    <h2><a name="make_function_output_iterator">The Function Output Iterator Object
    Generator</a></h2>

    The <tt>make_function_output_iterator()</tt> function provides a
    more convenient way to create function output iterator objects. The
    function saves the user the trouble of explicitly writing out the
    iterator types. If the default argument is used, the function
    type must be provided as an explicit template argument.

    <blockquote>
<pre>
template &lt;class UnaryFunction&gt;
function_output_iterator&lt;UnaryFunction&gt;
make_function_output_iterator(const UnaryFunction&amp; f = UnaryFunction())
</pre>
    </blockquote>

    <hr>

    <p>&copy; Copyright Jeremy Siek 2001. Permission to copy, use,
    modify, sell and distribute this document is granted provided this
    copyright notice appears in all copies. This document is provided
    "as is" without express or implied warranty, and with no claim as
    to its suitability for any purpose.

</body>
</html>