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

C++11 新特性

2016-09-22 10:25 267 查看
【1】新关键字 auto

作用:自动类型推导

auto a////auto是通过初始化表达式进行类型推导,如果没有初始化表达式,就无法确定a的类型
auto i=1;
auto d=1.0;
auto str="hello world";
auto ch='A';
auto ite=iv.begin();

auto p=new foo()//自定义类型推导


【2】序列for循环

作用:用于遍历数组,容器等

void ShowVec(const vector<int>& valList)
{
for (auto iter = valList.cbegin(); iter != valList.cend(); iter++)
{
cout << (*iter) << endl;
}
}

void ShowVec(const vector<int>& valList)
{
for (auto val : valList)
{
cout << val << endl;
}
}


【3】初始化方法

//支持如下方式的初始化方法
int arr[3]{1, 2, 3};
vector<int> iv{1, 2, 3};
map<int, string>{{1, "a"}, {2, "b"}};
string str{"Hello World"};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: