Boost logo

Boost Users :

From: Jeff F (TriumphSprint2000_at_[hidden])
Date: 2006-11-20 07:58:50


Terry G wrote:
> This doesn't compile.
> How best to accomplish this using generic STL or Boost
> algorithms/adapters?

IIRC, you'll need to use boost lambda facilities:
http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.construction_and_destruction

or boost::spirit::phoenix. Sorry I don't have a link for this one.

Or use the techniques below:

> terry
>
> #include <iostream>
> #include <iomanip>
> #include <algorithm>
> #include <vector>
> #include <iterator>
> #include <cstdlib>
>
> #include <boost/bind.hpp>
> #include <boost/mem_fn.hpp>
>
> using namespace std;
> using namespace boost;
>
> class MyInt {
> int _value;
> public:
> explicit MyInt(int x=0) : _value(x) { }
> int value() const { return _value; }

    static Construct(int x=0){ return MyInt(x); }

> }; // MyInt
>
> int main() {
> vector<MyInt> v;
>
> // Do this:
> // for (int i=0; i!=30; ++i) v.push_back(MyInt(rand()));
> generate_n(back_inserter(v), 30, bind(MyInt, bind(rand)));

    generate_n(back_inserter(v), 30, bind(&MyInt::Construct, bind(rand)));

>
> // Now print v.
> transform(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"),
> mem_fn(&MyInt::value));
>
> return 0;
> } // main

Jeff Flinn


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net