David Nehme
David Nehme

Reputation: 21597

inverse of boost::make_function_output_iterator

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

Upvotes: 3

Views: 715

Answers (3)

David Nehme
David Nehme

Reputation: 21597

The class

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

Converts an output iterator to a unary function.

typedef vector<int> t_vec;
t_vec v;
t_vec v2;
for_each(v.begin(), v.end(), 
         make_iterator_function(back_inserter(v2));

Upvotes: 1

If you have the option I'd do this with lambdas rather than std/boost::bind, e.g.:

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

Of course it would make more sense to just use std::copy in the first place!

Upvotes: 1

Rafał Rawicki
Rafał Rawicki

Reputation: 22690

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

Upvotes: 1

Related Questions