Can I use STL transform and lambda to add 2 int vector?
Hi, I would like to know if I can use transform and lambda to add 2 int vector and save the result into a third int vector vector<int> src1; vector<int> src2; vector<int> result // put the value of src1 and src2
Meryl Silverburgh wrote:
I would like to know if I can use transform and lambda to add 2 int vector and save the result into a third int vector
vector<int> src1; vector<int> src2;
vector<int> result // put the value of src1 and src2
Not tested, but something like this: // Resize result to accommodate elements result.resize(src1.size()); // Add elements of both vectors and store result in third vector. // Assumes src1 and src2 have the same size. std::transform(src1.begin(), src1.end(), src2.begin(), result.begin(), (boost::lambda::_1 + boost::lambda::_2)); -delfin
Meryl Silverburgh wrote:
I would like to know if I can use transform and lambda to add 2 int vector and save the result into a third int vector
vector<int> src1; vector<int> src2;
vector<int> result // put the value of src1 and src2
Yes. using boost::lambda::_1; using boost::lambda::_2; std::transform(src1.begin(), src1.end(), src2.begin(), std::backinserter(result), _1 + _2); KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com
On 3/2/06, Meryl Silverburgh
Hi,
I would like to know if I can use transform and lambda to add 2 int vector and save the result into a third int vector
vector<int> src1; vector<int> src2;
vector<int> result // put the value of src1 and src2
You could, but that's simple enough that you don't even need lambda: assert( src1.size() == src2.size() ); result.resize( src1.size() ); std::transform( src1.begin(), src1.end(), src2.begin(), std::add<int>() ); The only thing lambda would do for you would be to let you say _1 + _2 instead of add<int>(). ~ Scott
On 3/2/06, me22
assert( src1.size() == src2.size() ); result.resize( src1.size() ); std::transform( src1.begin(), src1.end(), src2.begin(), std::add<int>() );
Sorry for the noise, that transform call should be the following instead: std::transform( src1.begin(), src1.end(), src2.begin(), result.begin(), std::add<int>() ); ~ Scott
participants (4)
-
Delfin Rojas
-
Kevin Heifner
-
me22
-
Meryl Silverburgh