The function output iterator adaptor makes it easier to create custom output iterators. The adaptor takes a Unary Function and creates a model of Output Iterator. 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.
namespace boost { template <class UnaryFunction> class function_output_iterator; template <class UnaryFunction> function_output_iterator<UnaryFunction> make_function_output_iterator(const UnaryFunction& f = UnaryFunction()) }
#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; }
The function_output_iterator class creates an Output Iterator out of a Unary Function. Each item assigned to the output iterator is passed as an argument to the unary function.template <class UnaryFunction> class function_output_iterator;
Parameter | Description |
---|---|
UnaryFunction | The function type being wrapped. The return type of the function is not used, so it can be void. The function must be a model of Unary Function. |
explicit function_output_iterator(const UnaryFunction& f = UnaryFunction())
template <class UnaryFunction> function_output_iterator<UnaryFunction> make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
© 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.