c++boost.gif (8819 bytes)

Function Output Iterator Adaptor

Defined in header boost/function_output_iterator.hpp

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.

Synopsis

namespace boost {
  template <class UnaryFunction>
  class function_output_iterator;

  template <class UnaryFunction>
  function_output_iterator<UnaryFunction>
  make_function_output_iterator(const UnaryFunction& f = UnaryFunction())
}

Example

In this example we create an output iterator that appends each item onto the end of a string, using the string_appender function.
#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

template <class UnaryFunction>
class function_output_iterator;
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 Parameters

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.

Concept Model

The function output iterator class is a model of Output Iterator.

Members

The function output iterator implements the member functions and operators required of the Output Iterator concept. In addition it has the following constructor:
explicit function_output_iterator(const UnaryFunction& f = UnaryFunction())



The Function Output Iterator Object Generator

The make_function_output_iterator() 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.
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.