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循环更简洁(安全)。
#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循环更简洁(安全)。
相关文章推荐
- C++11新特性:range based for loop-范围for循环基本使用方法
- c++11 : range-based for loop
- C++11新特性之四:range-based for loops
- C++11 | range-based for loop
- c++11 : range-based for loop
- 一些C++11语言新特性 - Range-Based for Loops
- C++11 新特性之Range-based for loops
- C++11新特性之基本范围的For循环(range-based-for)
- 自定义数据结构的基于范围的for循环 range-based for loop
- C++11中range-based for loops中&&与&的区别
- C++11 range-based for loops
- Modern C++(二)Range-based for loop(基于范围的for循环)
- C++11 性能Range-based for loops
- C++11特性——基于范围的for语句
- Range-Based for Loops
- Code::Blocks error: range-based 'for' loops are not allowed in C++98 mode
- 关于C++11 range-for的一个陷阱
- 【C++11】新特性——常量表达式constexpr
- C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)
- C++11 标准新特性:Defaulted 和 Deleted 函数及constexpr