2

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.

4

1 に答える 1

8

C++11 ラムダには少なくとも GCC/G++ 4.5 が必要です。G++ 4.4 では動作しません。

http://gcc.gnu.org/projects/cxx0x.html (または、現在ダウンしているため、キャッシュされたバージョン)を参照してください。

于 2013-03-19T01:13:07.970 に答える