Re: Is there an STL algo to fill a vector with product of 2 other
vectors?
On Dec 9, 7:54 pm, Steve555 <foursh...@btinternet.com> wrote:
On 9 Dec, 17:09, James Kanze <james.ka...@gmail.com> wrote:
On Dec 9, 3:43 pm, Steve555 <foursh...@btinternet.com> wrote:
I'm looking for an algorithm that takes 2 vectors and a
function pointer as arguments? I want to use the function
pointer to calculate a result e.g. the product, and put
the result either in place, or in a third vector. I've
looked through the list of algorithms and there doesn't
appear to be one. transform() is the closest, but alas
works on only a single vector.
There are two versions of std::transform. The following code
should do the trick:
assert( v1.size() == v2.size() ) ;
std::transform( v1.begin(), v1.end(),
v2.begin(),
std::back_inserter( v3 ),
std::multiplies< double >() ) ;
Thanks James, but I wasn't sure why you used
std::back_inserter()?
Just to have an example. In the above code, the results are put
into a third, initially empty array. If you want them to
replace the contents of one of the original arrays, you
v1.begin() or v2.begin() instead. (The advantage of using
back_inserter here is that it makes it clear that this argument
is the output, since a back_insert_iterator can only be used for
output.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34