Angus Leeming wrote:
> -       vector<string> nvec;
> -       std::copy(bstore.begin(), bstore.end(),
> -                 lyx::back_inserter_fun(nvec, &Buffer::fileName));
> +       vector<string> nvec(
> +               make_transform_iterator(bstore.begin(),
> +                                       bind(&Buffer::fileName,
> _1)),
> +               make_transform_iterator(bstore.end(),
> +                                       bind(&Buffer::fileName, _1))
> +               );
> 
> It's not exactly readable is it ;-)
> 
> I've been using Boost.Spirit a lot at work recently and so have
> become quite comfortable with Spirit's phoenix library (a better
> lambda?)
> 
> Try this for size:

Even nicer, me thinks. Won't the std::transform call also work with 
boost.bind? I think it will. No lambda and a LOT more readable than 
the code above...

Angus



#include <boost/spirit/phoenix.hpp>
#include <iostream>
#include <string>
#include <vector>

struct Buffer {
    Buffer(std::string const & name) : name_(name) {}
    std::string const & name() const { return name_; }
private:
    std::string name_;
};

char const * const names[] = { "angus", "ben", "chris", "dave" };
int const size_names = sizeof(names) / sizeof(names[0]);

int main () {
    std::vector<Buffer> bstore(names, names + size_names);
    std::vector<std::string> nvec(bstore.size());

    using phoenix::arg1;
    using phoenix::bind;

    std::transform(bstore.begin(), bstore.end(), nvec.begin(),
                   bind(&Buffer::name)(arg1));

    std::for_each(nvec.begin(), nvec.end(), std::cout << arg1 << ' ');
    std::cout << std::endl;

    return 0;


-- 
Angus

Reply via email to