mirror of
https://github.com/boostorg/utility.git
synced 2025-05-08 10:24:00 +00:00
added function output iterator adaptor
[SVN r9351]
This commit is contained in:
parent
b5ed77985e
commit
27dfb25570
41
fun_out_iter_example.cpp
Normal file
41
fun_out_iter_example.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// (C) Copyright Jeremy Siek 2001. Permission to copy, use, modify,
|
||||||
|
// sell and distribute this software is granted provided this
|
||||||
|
// copyright notice appears in all copies. This software is provided
|
||||||
|
// "as is" without express or implied warranty, and with no claim as
|
||||||
|
// to its suitability for any purpose.
|
||||||
|
|
||||||
|
// Revision History:
|
||||||
|
|
||||||
|
// 27 Feb 2001 Jeremy Siek
|
||||||
|
// Initial checkin.
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <boost/function_output_iterator.hpp>
|
||||||
|
|
||||||
|
struct string_appender {
|
||||||
|
string_appender(std::string& s) : m_str(s) { }
|
||||||
|
void operator()(const std::string& x) const {
|
||||||
|
m_str += x;
|
||||||
|
}
|
||||||
|
std::string& m_str;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int, char*[])
|
||||||
|
{
|
||||||
|
std::vector<std::string> 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 << s << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
169
function_output_iterator.htm
Normal file
169
function_output_iterator.htm
Normal file
@ -0,0 +1,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 <class UnaryFunction>
|
||||||
|
class function_output_iterator;
|
||||||
|
|
||||||
|
template <class UnaryFunction>
|
||||||
|
function_output_iterator<UnaryFunction>
|
||||||
|
make_function_output_iterator(const UnaryFunction& 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 <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <boost/function_output_iterator.hpp>
|
||||||
|
|
||||||
|
struct string_appender {
|
||||||
|
string_appender(std::string& s) : m_str(s) { }
|
||||||
|
void operator()(const std::string& x) const {
|
||||||
|
m_str += x;
|
||||||
|
}
|
||||||
|
std::string& m_str;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int, char*[])
|
||||||
|
{
|
||||||
|
std::vector<std::string> 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 << s << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2><a name="function_output_iterator">The Function Output Iterator Class</a></h2>
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<pre>
|
||||||
|
template <class UnaryFunction>
|
||||||
|
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 <class UnaryFunction>
|
||||||
|
function_output_iterator<UnaryFunction>
|
||||||
|
make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
|
||||||
|
</pre>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<p>© 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>
|
@ -84,6 +84,11 @@
|
|||||||
"../../boost/counting_iterator.hpp">boost/counting_iterator.hpp</a></tt><br>
|
"../../boost/counting_iterator.hpp">boost/counting_iterator.hpp</a></tt><br>
|
||||||
|
|
||||||
<a href="counting_iterator.htm">Counting Iterator Adaptor</a>
|
<a href="counting_iterator.htm">Counting Iterator Adaptor</a>
|
||||||
|
|
||||||
|
<li>Header <tt><a href=
|
||||||
|
"../../boost/function_output_iterator.hpp">boost/function_output_iterator.hpp</a></tt><br>
|
||||||
|
|
||||||
|
<a href="function_output_iterator.htm">Function Output Iterator Adaptor</a>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p><b><a href="http://www.boost.org/people/dave_abrahams.htm">Dave
|
<p><b><a href="http://www.boost.org/people/dave_abrahams.htm">Dave
|
||||||
@ -98,8 +103,9 @@
|
|||||||
<b><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy
|
<b><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy
|
||||||
Siek</a></b> contributed the <a href="transform_iterator.htm">transform
|
Siek</a></b> contributed the <a href="transform_iterator.htm">transform
|
||||||
iterator</a> adaptor, the integer-only version of <tt><a href=
|
iterator</a> adaptor, the integer-only version of <tt><a href=
|
||||||
"counting_iterator.htm">counting_iterator_generator</a></tt>, and most of
|
"counting_iterator.htm">counting_iterator_generator</a></tt>,
|
||||||
the documentation.<br>
|
the <a href="function_output_iterator.htm">function output iterator</a>
|
||||||
|
adaptor, and most of the documentation.<br>
|
||||||
<b><a href="http://www.boost.org/people/john_potter.htm">John
|
<b><a href="http://www.boost.org/people/john_potter.htm">John
|
||||||
Potter</a></b> contributed the <tt><a href=
|
Potter</a></b> contributed the <tt><a href=
|
||||||
"projection_iterator.htm">projection_</a></tt> and <tt><a href=
|
"projection_iterator.htm">projection_</a></tt> and <tt><a href=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user