您的位置:首页 > 编程语言 > C语言/C++

C++11中的有趣的新特性(constexpr ) (Range-based for loop)

2014-11-04 23:22 417 查看
#include <iostream>

#include<stdexcept>

using namespace std;

//constexpr keyword

constexpr int sqare(int tmp)

{

return tmp*tmp;

}

int main()

{

int myArray[sqare(2)]={1,2,3,4};

//Range-based for loop

for(int &tmp : myArray)

{

tmp++;

}

for(int tmp : myArray)

{

cout<<tmp<<endl;

}

return 0;

}

执行结果:(点击可放大)



可以看出:constexpr 比宏更灵活,而Range-based for loop比旧式for循环更简洁(安全)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: