标签存档: C

Linux C 常用函数

Link

Lambda vs. Function class

#define LAMBDA
#include

#ifdef LAMBDA
int main()
{
int v[10];
for (int i = 0; i < 10; ++i)
v[i] = i;

int cnt = 0;
std::for_each(v, v + 10, [&cnt](int i){
if (i % 2)
{
wprintf(L”even %d\r\n”, i);
++cnt;
}
});

return 0;
}
#else
class Func
{
public:
Func(int& i)
: _i(i)
{}

void operator()(int i)
{
if (i % 2)
{
wprintf(L”even %d\r\n”, i);
++_i;
}
}

private:
int& _i;
};

int main()
{
int v[10];
for (int i = 0; i < 10; ++i)
v[i] = i;

int cnt = 0;
std::for_each(v, v + 10, Func(cnt));

return 0;
}
#endif