Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use lambda in for_each?

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;
}
like image 622
Atul Avatar asked Jul 16 '12 10:07

Atul


2 Answers

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();
});
like image 157
R. Martinho Fernandes Avatar answered Sep 25 '22 18:09

R. Martinho Fernandes


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.

like image 22
Nawaz Avatar answered Sep 23 '22 18:09

Nawaz