Christopher Smith on 15 Aug 2003 16:01:07 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] bound for success!


On Fri, 2003-08-15 at 08:25, Gavin Doughtie wrote:
> OK, I want to be Arlo and write the following code:
> 
> for(unsigned int i(0); i < mySourceVect.size(); ++i) {
> 	myDestVect.push_back(aTransformationFunction(mySourceVect[i]));
> }

Shame on you for using indexes instead of iterators. ;-)

> with std::for_each and std::generate and boost::bind. More specifically, 
> ow does one get an iterated element from mySourceVect as the input 
> parameter to a bound functor for std::generate?

I'm not entirely sure I understand what what you are trying to
accomplish here, but it looks like you are trying to do something like
Samlltalk's collect. I've always wondered why there wasn't a clear STL
analogue to that. I'd suggest making one of your own (somewhat pseudo
codish here):

template<typename InputIterator,
         typename OutputIterator,
         typename Predicate>
OutputIterator collect(InputIterator first,
                       InputIterator last,
                       OutputIterator result,
                       Predicate pred) {
    for(InputIterator i = first, i != last; ++i) {
        *result++ = pred(*i);
    }
    return result;
}

collect(mySourceVect.begin(),
        mySourceVect.end(),
        back_insert_iterator(myDestVect),
        myTransFormationFunction);

One misses having collect often enough that I think it's worth writing
something for it.

-- 
Christopher Smith <x@xxxxxxxx>
_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp