的boost :: make_function_output_iterator逆 [英] inverse of boost::make_function_output_iterator
问题描述
升压功能 make_function_output_iterator 一个转换功能将适合的std :: for_each的到适合的std ::复制的迭代器。有没有做反向升压功能。也就是说,采取适当的迭代器的std ::副本,并将其转换为功能适合的std :: for_each的。
所以,如果我有一个输出迭代器output_iter。我需要
的for_each(v1.begin(),v1.end(),make_output_iterator_function(output_iter));
要做到同样的事情。
拷贝(v1.begin(),v1.end(),output_iter);
的std :: insert_iterator
或 的std ::变换
是你正在寻找什么?
您可以绑定的std :: insert_iterator ::运算符=
,它不插入每次调用,具有一定的的boost ::绑定
巫术:
的#include<升压/ bind.hpp>
#包括LT&;矢量>
#包括LT&;&迭代器GT;
#包括LT&;&算法GT; 的typedef的std ::矢量<&INT GT;容器;
的typedef的std :: insert_iterator<集装箱> InsertIt;诠释主(){
集装箱V1,V2;
InsertIt insert_it(V2,v2.end());
的std :: for_each的(v1.begin(),v1.end()
提高::绑定(的static_cast< InsertIt及(InsertIt :: *)(typename的集装箱::为const_reference)GT;(安培; InsertIt ::运算符=),insert_it,_1));
}
The boost function make_function_output_iterator converts a function that would be appropriate for std::for_each into an iterator appropriate for std::copy. Is there a boost function that does the reverse. That is, takes an iterator appropriate for std::copy and converts it to a function appropriate for std::for_each.
So if I have an output iterator output_iter. I need
for_each(v1.begin(), v1.end(), make_output_iterator_function(output_iter));
To do the same thing as
copy(v1.begin(), v1.end(), output_iter);
Maybe std::insert_iterator
or std::transform
are what you are looking for?
You can bind std::insert_iterator::operator=
, which does an insert on every invocation, with some boost::bind
sorcery:
#include <boost/bind.hpp>
#include <vector>
#include <iterator>
#include <algorithm>
typedef std::vector<int> Container;
typedef std::insert_iterator< Container > InsertIt;
int main(){
Container v1, v2;
InsertIt insert_it (v2,v2.end());
std::for_each(v1.begin(), v1.end(),
boost::bind(static_cast<InsertIt& (InsertIt::*)(typename Container::const_reference)>(&InsertIt::operator=), insert_it, _1));
}
这篇关于的boost :: make_function_output_iterator逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!