Wie verwenden von lambda in for_each?

Ich versuche, mit for_each statt der normalen for-Schleife. Aber nun, da ich bin neu in C++11, bin ich irgendwie stecken. Meine Absicht hier ist die Verwendung von for_each-und lambda-Ausdrücke zusammen. Irgendwelche Ideen ? Ich benutze visual studio 2010.
Grüße,
Atul

Hier ist der 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;
}
ich habe in beiden for_each-Schleifen Punkt ist, schreiben Sie einfach [](Point& ich) { i(); }

InformationsquelleAutor Atul | 2012-07-16

Schreibe einen Kommentar