C++ boost的逆函数::make_函数\u输出\u迭代器

C++ boost的逆函数::make_函数\u输出\u迭代器,c++,boost,stl,iterator,C++,Boost,Stl,Iterator,boost函数将适用于std::for_each的函数转换为适用于std::copy的迭代器。是否有一个反方向的增压功能。也就是说,获取适合std::copy的迭代器,并将其转换为适合std::for_的函数 所以如果我有一个输出迭代器output_iter。我需要 for_each(v1.begin(), v1.end(), make_output_iterator_function(output_iter)); 做与…相同的事 copy(v1.begin(), v1.end(), outp

boost函数将适用于std::for_each的函数转换为适用于std::copy的迭代器。是否有一个反方向的增压功能。也就是说,获取适合std::copy的迭代器,并将其转换为适合std::for_的函数

所以如果我有一个输出迭代器output_iter。我需要

for_each(v1.begin(), v1.end(), make_output_iterator_function(output_iter));
做与…相同的事

copy(v1.begin(), v1.end(), output_iter);
也许或者你正在寻找什么

您可以绑定
std::insert\u iterator::operator=
,它会在每次调用时执行插入操作,并使用一些
boost::bind
魔法:

#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));
}
#包括
#包括
#包括
#包括
向量容器;
typedef std::insert_迭代器InsertIt;
int main(){
容器v1、v2;
InsertIt insert_it(v2,v2.end());
std::for_each(v1.begin(),v1.end(),
boost::bind(static_cast(&InsertIt::operator=),insert_it,_1));
}
这门课

 template<typename T>
 struct iterator_to_function {
      T iter_;
      iterator_to_function(const T& iter) : iter_(iter) {}
      template<typename T2>
      T& operator()(const T2& elem) {*iter_=elem; return ++iter_;}
 };

template<class T>
iterator_to_function<T> make_iterator_function(const T& iter) 
{
  return iterator_to_function<T>(iter);
}
模板
结构迭代器到函数{
国际热核试验堆;
迭代器到函数(const T&iter):iter(iter){
模板
T&运算符()(常数T2&elem){*iter=elem;return++iter}
};
模板
迭代器到函数生成迭代器函数(const T&iter)
{
将迭代器返回到函数(iter);
}
将输出迭代器转换为一元函数

typedef vector<int> t_vec;
t_vec v;
t_vec v2;
for_each(v.begin(), v.end(), 
         make_iterator_function(back_inserter(v2));
typedef向量t_vec;
t_vec v;
t_vec v2;
对于每个(v.begin(),v.end(),
生成迭代器函数(返回插入器(v2));

如果您有选择,我会使用lambdas而不是
std/boost::bind
,例如:

std::vector<type> v1;
std::for_each(v1.begin(), v1.end() [&output_iter](const type& t) {*output_iter++ = t});
std::vector v1;
std::for_each(v1.begin()、v1.end()[&output_iter](const type&t){*output_iter++=t});

当然,首先只使用
std::copy
会更有意义!

我不太明白。这两个东西是完全不同的。每个
都没有
std::for_的输出。也许你在寻找:或者
std::transform
?你能添加第一种情况的示例吗它显示了第二个函数所需的内容?如果迭代器(例如)只是一个指针,那么它不会工作,对吗?对于我认为的一般情况,输出迭代器需要在赋值之间递增。@awoodland您是对的。对于标准容器,++被忽略,但是对于原始指针,std::copy也应该工作。