Jason Fredrickson on 15 Aug 2003 16:31:47 -0000


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

FW: [ALACPP] bound for success!


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

Correct me if I'm wrong, but isn't this exactly what std::transform is
designed to let you do?  So you'd want something like:

std::transform(mySourceVect.begin(),
			mySourceVect.end(),
			std::back_inserter(myDestVect),*
			boost::bind(&aTransformationFunction, _1));

* I don't remember exactly what the back insertion syntax is for a
vector.


Transform is designed to iterate one range and call the function on each
element, then put the output into the output iterator.  For_each is
designed to iterate a range and call the function, but the output is
just dropped.  Generate is designed to assign the output of a
parameterless function.

All this, of course, assumes that your transformation function is an
actual function, and you're not trying to create it in-line...

Jason

Jason Fredrickson
Vice-President, Operations
Agile Solutions
Jason.Fredrickson@xxxxxxxxxxxx
(626) 446-8925

> 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?
> 
> Personally, the for loop looks the most compact and obvious, but I'm
> trying to get these other tools in more widespread use at work.
> 
> Sample code, anyone?
> 
> Gavin
> 
> _______________________________________________
> alacpp mailing list
> alacpp@xxxxxxxxxxx http://lists.ellipsis.cx/mailman/listinfo/alacpp
> 


_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp