Skip to content

Instantly share code, notes, and snippets.

@mbohun
Created August 31, 2012 01:55
Show Gist options
  • Save mbohun/3547705 to your computer and use it in GitHub Desktop.
Save mbohun/3547705 to your computer and use it in GitHub Desktop.
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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment