Code::Blocks设置支持C++ 11

时间:2021-01-23 16:58:33

进入codeblocks,点击Settings --> Compiler..,进入如下页面

Code::Blocks设置支持C++ 11
勾选“Have g++ follow the C++11 ISO language standard [-std=c++11]”并确定。
可以通过如下示例来查看效果:

#include <iostream>
#include <functional>
using namespace std;
int add(int a, int b)
{
    return a + b;
}
int main()
{
    function<int(int,int)> f = add;
    cout << f(10, 20) << endl;
    auto g = []{ return "Hello World"; };
    cout << g() << endl;
    int a[5] = { 1, 2, 3, 4, 5 };
    for (const int & num : a)
    {
        cout << num << endl;
    }
    return 0;
}