| 作者 | 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. |
| 摘要 | 函数输出迭代器适配器使得创建自定义输出迭代器更加容易。该适配器接受一个一元函数并创建一个输出迭代器模型。分配给输出迭代器的每个项目都作为参数传递给一元函数。此迭代器的动机是,创建符合标准的输出迭代器并非易事,特别是因为正确的实现通常需要一个代理对象。 |
|---|
目录
#include <boost/iterator/function_output_iterator.hpp>
template <class UnaryFunction>
class function_output_iterator {
public:
typedef std::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit function_output_iterator();
explicit function_output_iterator(const UnaryFunction& f);
/* see below */ operator*();
function_output_iterator& operator++();
function_output_iterator& operator++(int);
private:
UnaryFunction m_f; // exposition only
};
UnaryFunction必须是 Assignable 和 Copy Constructible。
function_output_iterator是 Writable 和 Incrementable 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;
}