Boost C++ 库

……世界上最受推崇且设计最专业的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu,《C++ 编码规范

函数输出迭代器 - Boost C++ 函数库

函数输出迭代器

作者 David Abrahams, Jeremy Siek, Thomas Witt
联系方式 dave@boost-consulting.com, jsiek@osl.iu.edu, witt@ive.uni-hannover.de
组织 Boost Consulting, 印第安纳大学 Open Systems Lab, 汉诺威大学 运输铁路运营与建设研究所
日期 2006-09-11
版权 Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.
摘要 函数输出迭代器适配器使得创建自定义输出迭代器更加容易。该适配器接受一个一元函数并创建一个输出迭代器模型。分配给输出迭代器的每个项目都作为参数传递给一元函数。此迭代器的动机是,创建符合标准的输出迭代器并非易事,特别是因为正确的实现通常需要一个代理对象。

function_output_iteratorrequirements

UnaryFunction必须是 Assignable 和 Copy Constructible。

function_output_iterator模型

function_output_iterator是 Writable 和 Incrementable Iterator 概念的模型。

function_output_iterator操作

explicit function_output_iterator(const UnaryFunction& f = UnaryFunction());

效果构造一个实例function_output_iterator带有m_f由...构造f.

operator*();

返回一个对象r类型未指定,使得r = t等效于m_f(t)对于所有t.

function_output_iterator& operator++();

返回*this

function_output_iterator& operator++(int);

返回*this

示例

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;
}