2013-03-21 80 views
-2

試圖傳遞lambda函數進行排序向量C++

vector<int> i; 

i.push_back(1); 
i.push_back(2); 
i.push_back(5); 
i.push_back(6); 


std::sort(i.begin(),i.end() , [](int a, int b)->bool{ return a < b; }); 

引發錯誤

hello.cpp: In function ‘int main()’: 
hello.cpp:63:69: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x [enabled by default] 
hello.cpp:63:70: error: no matching function for call to ‘sort(std::vector<int>::iterator, std::vector<int>::iterator, main()::<lambda(int, int)>)’ 
hello.cpp:63:70: note: candidates are: 
/usr/include/c++/4.6/bits/stl_algo.h:5317:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter) 
/usr/include/c++/4.6/bits/stl_algo.h:5353:5: note: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare) 

不能完全確定是什麼問題。
gcc版本4.6.1

+3

您是否嘗試將建議的命令行參數添加到gcc調用中? – Stephan 2013-03-21 08:07:43

+0

'警告:lambda表達式只有-std = C++ 0x或-std = gnu ++ 0x [默認啓用]纔可用,這有點令人費解。你是否真的用這兩個選項編譯? – NPE 2013-03-21 08:07:45

+1

你需要升級你的編譯器(到目前爲止發佈的GCC 4.7或者甚至4.8)並且將'-std = C++ 11'標誌傳遞給升級後的編譯器。 GCC 4.6在C++ 2011標準發佈之前發佈,因此您確實希望GCC 4.7至少能夠使用C++ 2011 lambda。 – 2013-03-21 08:08:03

回答

-1

即使他們是警告聽他們的虛擬。