I am trying to use for_each instead of the normal for loop. However, since I am new to C++11, I am kind of stuck. My intent here is to use for_each and lambda expression together. Any ideas ? I am using visual studio 2010.
Regards,
Atul
Here is the code.
#include "stdafx.h"
#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
using namespace std;
struct Point
{
union
{
double pt[3];
struct {double X,Y,Z;};
struct {double x,y,z;};
};
Point(double x_,double y_,double z_)
:x(x_),y(y_),z(z_)
{}
Point()
:x(0.0),y(0.0),z(0.0)
{}
void operator()()
{
cout << "X coordinate = " << x << endl;
cout << "Y coordinate = " << y << endl;
cout << "Z coordinate = " << z << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<Point> PtList(100);
//! the normal for loop
for(int i = 0; i < 100; i++)
{
// Works well
PtList[i]();
}
//! for_each using lambda, not working
int i = 0;
for_each(PtList.begin(),PtList.end(),[&](int i)
{
// Should call the () operator as shown previously
PtList[i]();
});
//! for_each using lambda, not working
Point CurPt;
for_each(PtList.begin(),PtList.end(),[&CurPt](int i)
{
cout << "I = " << i << endl;
// should call the() operator of Point
CurPt();
});
return 0;
}
The third parameter of for_each
is a function to apply to each element, not to each index. Otherwise, what would be the point of using that over a traditional loop?
So, instead of an int
parameter, it takes a Point
parameter. And now there's no reason to capture anything, because a reference to PtList
is unnecessary.
// Should make operator() const as it doesn't modify anything
for_each(PtList.begin(),PtList.end(),[](Point const& p)
{
p();
});
Your std::for_each
is obviously wrong. The type of the argument to the lamba should be Point
, or Point const&
depending on what you want to do, and what you're allowed to do.
It should be this:
int count = 0;
for_each(PtList.begin(),PtList.end(), [&](Point const & p)
{
cout <<"No. " << ++count << endl;
p();
});
Make your operator()
a const member function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With