Gary Wessle wrote:
Hi
I have 2 valarray<doublea and b, how can I make a = 1/b, that is
each element of a is 1 / each element of b?
I have been reading around and could not understand how it is done.
let a and b have the same size.
transform(a.begin(), a.end(), &b[0], operation)
or
b = a /=(1);
not sure.
thanks
Well, it is a better idea to switch to vectors - as valarrays don't
have iterators, and thren transform is not usefull. An example then
would be:
#include <iostream>
#include <algorithm>
#include <vector>
double reciproke(const double& d)
{
return 1.0/d;
}
int main()
{
std::vector<doublea(2), b(2);
a[0] = 2.0;
a[1] = 4.0;
std::transform(a.begin(), a.end(), b.begin(), reciproke);
for(std::vector<double>::iterator it = a.begin(); it !=
a.end();++it)
{
std::cout << *it << std::endl;
}
for(std::vector<double>::iterator it = b.begin(); it !=
b.end();++it)
{
std::cout << *it << std::endl;
}
return 0;
}