mbohun
8/31/2012 - 1:55 AM

c++0x lambda

c++0x lambda

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
        vector<int> v = {0, 1, 2, 6, 6, 666};

        std::copy(v.begin(),
                  v.end(),
                  std::ostream_iterator<int>(std::cout, ","));
        std::cout << std::endl;

        int x = 42;
        for_each(v.begin(),
                 v.end(),
                 [&](int& i){ i+=x; ++x;});

        std::copy(v.begin(),
                  v.end(),
                  std::ostream_iterator<int>(std::cout, ","));

        std::cout << std::endl << x << std::endl;
}

/*

g++ -std=c++0x test_cpp0x-lambda-00.cpp

ldd a.out
        linux-gate.so.1 =>  (0xffffe000)
        libstdc++.so.6 => /usr/local/gcc-4.6.3/lib/libstdc++.so.6 (0x776ba000)
        libm.so.6 => /lib/libm.so.6 (0x77671000)
        libgcc_s.so.1 => /usr/local/gcc-4.6.3/lib/libgcc_s.so.1 (0x77656000)
        libc.so.6 => /lib/libc.so.6 (0x774f6000)
        /lib/ld-linux.so.2 (0x777a6000)

./a.out
0,1,2,6,6,666,
42,44,46,51,52,713,
48

*/