C++ sort using lambda errors

c++, lambda

Solution

C++11 lambdas require GCC/G++ 4.5 at least, they won't work with G++ 4.4.

See http://gcc.gnu.org/projects/cxx0x.html (or, since it is down right now, the cached version).

Problem

I'm trying to sort an array using a lambda to use another array in the comparison function. Inside a larger function, I have this code: ``` std::sort(arr.begin(), arr.end(),[] (int& a, int& b) { return (*i)[a] < (*i)[b]; }); ``` I get the following errors in the containing function: ``` mcmc.cpp:139: error: expected primary-expression before ‘[’ token mcmc.cpp:139: error: expected primary-expression before ‘]’ token mcmc.cpp:139: error: expected primary-expression before ‘int’ mcmc.cpp:139: error: expected primary-expression before ‘int’ ``` When compiling, I'm including the -std=c++0x option as well. I'm confused about what's going on. For some reason, it doesn't seem to recognize my syntax as valid. I did a yum update just in case, but it still seems as if it just doesn't recognize the use of lambdas.

Original source